Excel 无法打开从 c# 生成的
Excel not opening generated from c#
我正在尝试将数据表存储在 excel 中,它是在 c# (SSIS ScriptTask) 中生成的。
下面是我的代码:
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::ObjResultSet"].Value);
Excel.Application oXL = new Excel.ApplicationClass();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet;
oWB = oWBs.Count > 0 ? oWB = oWBs[0] : oWBs.Add(System.Reflection.Missing.Value);
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Excel.Worksheet)oWB.Worksheets[1];
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header time first only
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
oWB.SaveAs(Dts.Variables["User::ExcelPath"].Value, 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(false, Dts.Variables["User::ExcelPath"].Value, Missing.Value);
oWBs.Close();
oXL.Quit();
Dts.TaskResult = (int)ScriptResults.Success;
我遇到的问题是,生成了excel。但是当我尝试生成 .xls
时,它完全可以正常工作。但是当我尝试生成 .xlsx
时,这不起作用。
谁能帮帮我?
警告 - 这是没有 SSIS 的纯 C# 尝试。
这是我前一段时间使用的一种方法,所以它可能根本没有任何好的实践。如果可能的话,其他人应该改进它,如果他们看到它。
这曾经有一个 DataTable
有 2 列。 1 个用于时间戳,1 个用于值。因此,您必须根据需要安装它。
private void createXLSXFromDataTable(System.Data.DataTable table, string path)
{
MemoryStream ms = new MemoryStream();
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("data");
worksheet.Cells["A1"].LoadFromDataTable(table,true);
worksheet.Column(1).Style.Numberformat.Format = "dd/mm/yyyy\ hh:mm";
worksheet.Column(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
byte[] excelData = package.GetAsByteArray();
ms.Write(excelData, 0, excelData.Length);
}
ms.Flush();
using (FileStream fs = File.Create(pfad))
{
ms.WriteTo(fs);
}
//open again with ms office,
//so the file will get saved correctly and
//all columns have the correct format
Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = false;
Workbooks wbooks = application.Workbooks;
wbooks.Open(pfad, Origin: XlPlatform.xlWindows);
Workbook wbook = application.ActiveWorkbook;
wbook.Save();
//quitting the services,
//after that delete/stop COM-connections of each object
wbook.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wbook);
wbooks.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wbooks);
application.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(application);
MessageBox.Show("Document successfully created.");
}
可能,您必须再次打开 excel 文件并将其保存在您的代码中。至少那个错误发生在我创建发布代码的时候,所以这就是我在代码中再次打开文件并保存它的原因。
尝试使用 Excel.XlFileFormat.xlOpenXMLWorkbook
而不是 Excel.XlFileFormat.xlWorkbookNormal
[已更新]
oWB.SaveAs(Dts.Variables["User::ExcelPath"].Value, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, Missing.Value, Missing.Value,Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value);
我正在尝试将数据表存储在 excel 中,它是在 c# (SSIS ScriptTask) 中生成的。
下面是我的代码:
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::ObjResultSet"].Value);
Excel.Application oXL = new Excel.ApplicationClass();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet;
oWB = oWBs.Count > 0 ? oWB = oWBs[0] : oWBs.Add(System.Reflection.Missing.Value);
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Excel.Worksheet)oWB.Worksheets[1];
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header time first only
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
oWB.SaveAs(Dts.Variables["User::ExcelPath"].Value, 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(false, Dts.Variables["User::ExcelPath"].Value, Missing.Value);
oWBs.Close();
oXL.Quit();
Dts.TaskResult = (int)ScriptResults.Success;
我遇到的问题是,生成了excel。但是当我尝试生成 .xls
时,它完全可以正常工作。但是当我尝试生成 .xlsx
时,这不起作用。
谁能帮帮我?
警告 - 这是没有 SSIS 的纯 C# 尝试。
这是我前一段时间使用的一种方法,所以它可能根本没有任何好的实践。如果可能的话,其他人应该改进它,如果他们看到它。
这曾经有一个 DataTable
有 2 列。 1 个用于时间戳,1 个用于值。因此,您必须根据需要安装它。
private void createXLSXFromDataTable(System.Data.DataTable table, string path)
{
MemoryStream ms = new MemoryStream();
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("data");
worksheet.Cells["A1"].LoadFromDataTable(table,true);
worksheet.Column(1).Style.Numberformat.Format = "dd/mm/yyyy\ hh:mm";
worksheet.Column(1).Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
byte[] excelData = package.GetAsByteArray();
ms.Write(excelData, 0, excelData.Length);
}
ms.Flush();
using (FileStream fs = File.Create(pfad))
{
ms.WriteTo(fs);
}
//open again with ms office,
//so the file will get saved correctly and
//all columns have the correct format
Microsoft.Office.Interop.Excel.Application application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = false;
Workbooks wbooks = application.Workbooks;
wbooks.Open(pfad, Origin: XlPlatform.xlWindows);
Workbook wbook = application.ActiveWorkbook;
wbook.Save();
//quitting the services,
//after that delete/stop COM-connections of each object
wbook.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wbook);
wbooks.Close();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wbooks);
application.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(application);
MessageBox.Show("Document successfully created.");
}
可能,您必须再次打开 excel 文件并将其保存在您的代码中。至少那个错误发生在我创建发布代码的时候,所以这就是我在代码中再次打开文件并保存它的原因。
尝试使用 Excel.XlFileFormat.xlOpenXMLWorkbook
而不是 Excel.XlFileFormat.xlWorkbookNormal
[已更新]
oWB.SaveAs(Dts.Variables["User::ExcelPath"].Value, Excel.XlFileFormat.xlOpenXMLWorkbook, Missing.Value, Missing.Value, Missing.Value, Missing.Value,Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value);