日期:2014-05-18  浏览次数:21112 次

Winform导出Excel2007怎么兼容2003???
我的编译环境是Excel2007没问题 。
可是在Excel2003的机器上运行报错(new Microsoft.Office.Interop.Excel.ApplicationClass();)。
高手指点一下啊


C# code


   private void MakeExcel()
        {
            string filePath = System.Windows.Forms.Application.StartupPath + "\\Board.xls";
            if (!File.Exists(filePath))
            {
               //******************************************************
                Microsoft.Office.Interop.Excel.Application xls_exp = null;               
                Microsoft.Office.Interop.Excel._Workbook xls_book = null;
                Microsoft.Office.Interop.Excel._Worksheet xls_sheet = null;
                object missing = System.Reflection.Missing.Value;
                try
                {
                    // string[] tt = new string[] { "客户简称", "客户姓名", "联系电话", "银行账号", "账户名称", "账户性质" };
                    xls_exp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    xls_exp.Visible = true;
                    xls_book = xls_exp.Workbooks.Add(missing);
                    xls_sheet = xls_book.Worksheets.Add(missing, missing, 1, missing) as Microsoft.Office.Interop.Excel._Worksheet; //(Microsoft.Office.Interop.Excel._Worksheet)xls_book.ActiveSheet;
                    xls_sheet.Name = "RelationerTable";
                    xls_sheet.Columns.NumberFormatLocal = "@";
                    xls_sheet.Cells[1, 1] = "客户简称";
                    xls_sheet.Cells[1, 2] = "客户姓名";
                    xls_sheet.Cells[1, 3] = "联系电话";
                    xls_sheet.Cells[1, 4] = "银行账号";
                    xls_sheet.Cells[1, 5] = "账户名称";
                    xls_sheet.Cells[1, 6] = "账户性质";
                    xls_book.SaveAs(filePath, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, missing, missing, missing, missing, missing);
                    xls_exp.Quit();

                    System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + "\\Board.xls");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
                finally 
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xls_exp);
                    xls_exp = null;
                    GC.Collect();
                }
                //******************************************************
            }
            else
            {
                System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + "\\Board.xls");
            }
        }



------解决方案--------------------
导出的时候没有格式选项么?xls/xlsx
------解决方案--------------------
改变导出的文件格式,改变文件扩展名

C# code

    public static void SaveToExcel(string content, string filename)
    {

        System.Web.HttpContext.Current.Response.Clear();
        System.Web.HttpContext.Current.Response.ClearHeaders();
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.Buffer = false;
        System.Web.HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "Attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, Encoding.UTF8) + ".xls");
        Sy