【发布时间】:2017-09-06 09:07:18
【问题描述】:
这不是重复阅读完整问题
我有控制台应用程序。
这里我想将数据集值导出到excel中
为此,我将使用它 我用过 microsoft.office.interop.excel.dll
并使用这个命名空间
使用 Excel = Microsoft.Office.Interop.Excel;
这是我的excel导出代码
private static bool ExportDataTableToExcel(DataTable dt, string filepath)
{
Excel.Application oXL;
Excel.Workbook oWB;
Excel.Worksheet oSheet;
Excel.Range oRange;
try
{
oXL = new Excel.Application();
oXL.Visible = true;
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Data";
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header the first time through
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
oRange = oSheet.get_Range(oSheet.Cells[1, 1],
oSheet.Cells[rowCount, dt.Columns.Count]);
oRange.EntireColumn.AutoFit();
oSheet = null;
oRange = null;
oWB.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
oWB.Close(Missing.Value, Missing.Value, Missing.Value);
oWB = null;
oXL.Quit();
}
catch
{
throw;
}
finally
{
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
return true;
}
当我运行我的代码时出现错误
无法加载文件或程序集 'office, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' 或其依赖项之一。系统找不到指定的文件。
我的电脑上没有安装 ms office。
我以为我没有在我的电脑上安装 MSOffice,所以只有我得到了错误。
如果这不是我在代码中出错的错误,谁能告诉我
【问题讨论】:
标签: c# asp.net dll office-interop excel-interop