C#控制输出excel文件。本机执行没问题,移到其他电脑报错

excel版本不同,我做的是用的Office2003的,移到了2007的电脑上报错。有没有办法可以让程序自动适应Office版本

这个没办法,最好用同一版本,不同版本的office,com组件等应该会有一些区别。

可以考虑用流的形式来输出excel文件
我写的一个excle导出工具类 可以实现把datatable导出成excel,用流的形式来做,适合各种环境。

 using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Web;
using System.Reflection;
namespace Culture2.Util
{
    public class ExcelHelp
    {
        /// <summary>
        /// DataTable通过流导出Excel
        /// </summary>
        /// <param name="dt">要导出的datatable</param>
        /// <param name="fileName">保存文件名(例如:a.xls)</param>
                /// <param name="fileName">保存路径</param>
        /// <returns></returns>
        public static bool StreamExport(DataTable dt, string fileName, string savePath)
        {
            if (dt.Rows.Count > 65535) //总行数大于Excel的行数 
            {
                throw new Exception("预导出的数据总行数大于excel的行数");
            }
            if (string.IsNullOrEmpty(fileName)) return false;

            StringBuilder content = new StringBuilder();
            StringBuilder strtitle = new StringBuilder();
            content.Append("<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>");
            content.Append("<head><title></title><meta http-equiv='Content-Type' content=\"text/html; charset=gb2312\">");
            //注意:[if gte mso 9]到[endif]之间的代码,用于显示Excel的网格线,若不想显示Excel的网格线,可以去掉此代码
            content.Append("<!--[if gte mso 9]>");
            content.Append("<xml>");
            content.Append(" <x:ExcelWorkbook>");
            content.Append("  <x:ExcelWorksheets>");
            content.Append("   <x:ExcelWorksheet>");
            content.Append("    <x:Name>Sheet1</x:Name>");
            content.Append("    <x:WorksheetOptions>");
            content.Append("      <x:Print>");
            content.Append("       <x:ValidPrinterInfo />");
            content.Append("      </x:Print>");
            content.Append("    </x:WorksheetOptions>");
            content.Append("   </x:ExcelWorksheet>");
            content.Append("  </x:ExcelWorksheets>");
            content.Append("</x:ExcelWorkbook>");
            content.Append("</xml>");
            content.Append("<![endif]-->");
            content.Append("</head><body><table style='border-collapse:collapse;table-layout:fixed;'><tr>");
            for (int i = 0; i < dt.Columns.Count; i++)
                content.Append("<td><b>" + dt.Columns[i].ColumnName + "</b></td>");

            content.Append("</tr>\n");

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                content.Append("<tr>");
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    object obj = dt.Rows[j][k];
                    Type type = obj.GetType();
                    if (type.Name == "Int32" || type.Name == "Single" || type.Name == "Double" || type.Name == "Decimal")
                    {
                        double d = obj == DBNull.Value ? 0.0d : Convert.ToDouble(obj);
                        if (type.Name == "Int32" || (d - Math.Truncate(d) == 0))
                            content.AppendFormat("<td style='vnd.ms-excel.numberformat:#,##0'>{0}</td>", obj);
                        else
                            content.AppendFormat("<td style='vnd.ms-excel.numberformat:#,##0.00'>{0}</td>", obj);
                    }
                    else
                        content.AppendFormat("<td style='vnd.ms-excel.numberformat:@'>{0}</td>", obj);
                }
                content.Append("</tr>\n");
            }
            content.Append("</table></body></html>");
            content.Replace("&nbsp;", "");
            CreateExecl(fileName, content.ToString(), savePath);
            return true;
        }
        private static void CreateExecl(string saveName, string content, string SavePath)
        {
            if (!Directory.Exists(SavePath))
                Directory.CreateDirectory(SavePath);

            string savePath = SavePath + "\\" + saveName;

            if (!File.Exists(savePath))
            {
                using (FileStream fs = File.Create(savePath))
                {
                    byte[] arr = Encoding.GetEncoding("gb2312").GetBytes(content);
                    fs.Write(arr, 0, arr.Length);
                }
            }
        }

    }
}

使用方法:
先将你要导出的excel表格构建成一个datatable (datatable列名就是表头的列名称),可以直接使用sql语句来构造
然后调用以下方法:

  ExcelHelp.StreamExport(dt,"1.xls","c:\\");

就可以了

装个office 没那么费劲。