首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Winform导出Excel2007如何兼容2003?

2012-09-01 
Winform导出Excel2007怎么兼容2003???我的编译环境是Excel2007没问题 。可是在Excel2003的机器上运行报错(n

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");        System.Web.HttpContext.Current.Response.Write(content);        System.Web.HttpContext.Current.Response.Flush();        System.Web.HttpContext.Current.Response.End();    } 


[解决办法]
你引入的excel的dll版本不对,2003和2007的dll不一样
[解决办法]
你的意思是既用了2007的特性,又想让2003能打开?
那就没办法了。
[解决办法]
excel2003肯定不支持高版本的啊,你把导出的那儿属性设置为xls的即可

热点排行