我使用以下代码来设置 Excel 文件的单元格值:
for (int i = 2; i < rowCount; i++)
{
for (int j = 1; j < columnCount; j++)
{
worksheet.Cells[i, j].Value = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
}
}
但是我不断得到
错误:HRESULT 异常:0x800A03EC
为什么会出现此错误以及如何修复它?
更新:
try
{
saveFileDialog1.Title = "Save as Excel File";
saveFileDialog1.FileName = "";
saveFileDialog1.Filter = "Excel Files(2003)|*.xls|Excel Files(2007)|*.xls";
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Microsoft.Office.Interop.Excel.Application worksheet = new Microsoft.Office.Interop.Excel.Application();
worksheet.Application.Workbooks.Add(Type.Missing);
worksheet.Columns.ColumnWidth = 20;
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
worksheet.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName.ToString());
worksheet.ActiveWorkbook.Saved = true;
worksheet.Quit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
我通常使用
Value2
属性(不是 Value
)
因此,您的代码将如下所示:
for (int i = 2; i < rowCount; i++)
{
for (int j = 1; j < columnCount; j++)
{
worksheet.Cells[i, j].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
}
}
https://www.codeproject.com/Questions/470089/Exception-from-HRESULT-x-A-EC-Error
看来你不是唯一一个: http://stackoverflow.com/questions/7099770/hresult-0x800a03ec-on-worksheet-range[^]。
根据帖子,当您使用 在 Excel 2007 或 2010 中打开的旧 (xls) 工作簿。如果是这种情况, 在访问之前,请先尝试将文件另存为新格式的工作簿 细胞。
试试这个代码
for (int i = 1; i < columnCount + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < rowCount -1; i++)
{
for (int j = 0; j < columnCount; j++)
{
worksheet.Cells[i+2, j+1].Value2 = enrollmentDataGrid.Columns[j].GetCellContent(enrollmentDataGrid.Items[i]);
}
}
当我在 C# 中使用 wpf 框架编写导出到 xls 时。我也遇到了这个问题,我用下面的代码解决了它。尝试从这段代码中看出
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Workbook workb = app.Workbooks.Open("c:\\Users\\Jack\\Documents\\Document1.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
worksheet = workb.Sheets[1];
// worksheet = workbook.Sheets[1];
//worksheet = workbook.ActiveSheet;
//worksheet.Name = "birinchi";
for (int i = 0; i < Table.Columns.Count; i++)
{
// worksheet.Cells[1, i+1] = Table.Columns[i].Header;
}
for (int i = 0; i < Table.Columns.Count; i++)
{
for (int j = 0; j < Table.Items.Count; j++)
{
TextBlock b = Table.Columns[i].GetCellContent(Table.Items[j]) as TextBlock;
worksheet.Cells[j + 2, i + 1] = b.Text;
//MessageBox.Show(b.Text);
}
}
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document";
dlg.DefaultExt = ".xlsx";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
workbook.SaveAs(dlg.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
app.Quit();