无锡网站建设网页制作,制作销售网站有哪些,微博推广渠道,wordpress分享服务器目录咨询区 mistrmark#xff1a;我的一个项目中有导出 excel 的功能#xff0c;但我发现运行代码的机器上一定要安装 Excel#xff0c;否则就找不到 Microsoft.Office.Interop.Excel #xff0c;导致运行报错#xff0c;请问如何解决#xff1f;回答区 Mike Webb#xff1a… 咨询区 mistrmark我的一个项目中有导出 excel 的功能但我发现运行代码的机器上一定要安装 Excel否则就找不到 Microsoft.Office.Interop.Excel 导致运行报错请问如何解决回答区 Mike Webb你可以在 nuget 上找一下 ExcelLibrary 包它是免费开源的参考如下代码//Create the data set and table
DataSet ds new DataSet(New_DataSet);
DataTable dt new DataTable(New_DataTable);//Set the locale for each
ds.Locale System.Threading.Thread.CurrentThread.CurrentCulture;
dt.Locale System.Threading.Thread.CurrentThread.CurrentCulture;//Open a DB connection (in this example with OleDB)
OleDbConnection con new OleDbConnection(dbConnectionString);
con.Open();//Create a query and fill the data table with the data from the DB
string sql SELECT Whatever FROM MyDBTable;;
OleDbCommand cmd new OleDbCommand(sql, con);
OleDbDataAdapter adptr new OleDbDataAdapter();adptr.SelectCommand cmd;
adptr.Fill(dt);
con.Close();//Add the table to the data set
ds.Tables.Add(dt);//Heres the easy part. Create the Excel worksheet from the data set
ExcelLibrary.DataSetHelper.CreateWorkbook(MyExcelFile.xls, ds);除了 ExcelLibrary 之外还有一些其他工具包EPPlusNPOISogger大家觉得用 Open XML SDK 2.0 怎么样它的好处不需要安装 Office。由 Microsoft 打造有正统的 MSDN 文档。只需要引用一个 dll 即可。SDK附带了很多工具比如diffvalidator 等等。Jan Källman如果你倾向于生成 xlsx 格式的excel可以试试github上的 EPPlus它是从 ExcelPackage 上发展而来的发展到今天已经完全重写了它支持 范围单元格样式图标图形图片自动过滤 等等其他一些实用功能。点评区 说实话前段时间在调试一个老项目时还真遇到了这种情况真是太坑了看样子得参考几位大佬提到的sdk重写个人推荐 EPPlus。