【问题标题】:Set Excel Cell value using C#使用 C# 设置 Excel 单元格值
【发布时间】:2022-02-28 18:03:50
【问题描述】:

我使用以下代码来设置 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

为什么会出现此错误以及如何解决?

【问题讨论】:

  • 你的excel表格的扩展名是什么?
  • @ArchitGoyal,它是 xlsx
  • 文件有多少行? stackoverflow.com/questions/7099770/…参考第二个答案
  • @ArchitGoyal,它取决于数据网格。我希望应用程序通过单击按钮将数据导出到 Excel 工作表中。所以它应该创建一个新的 Excel 文件并写入它
  • 另外,当我对数据进行硬编码时,代码可以正常工作,但是当我在问题中使用代码时,它会失败

标签: c# excel-interop


【解决方案1】:

更新:

  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);
            }

【讨论】:

  • 问题是我似乎无法使用enrollmentDataGrid.Rows[I].Cells[j]。 “行”部分不存在
  • @YiQin 是 Winforms 还是 Wpf?请添加相应的标签
  • 我编辑了上面的代码。请检查是否是你想要的
  • 感谢帮助,但我没有使用datagridview,只是使用datagrid。是否可以将 datagridview 方法应用于 datagrid?
【解决方案2】:

我通常使用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]);
    }
 }

【讨论】:

  • 使用Value2后仍然报错。但是感谢您的帮助!
【解决方案3】:

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) 工作簿。如果是这种情况, 在访问之前先尝试将文件另存为新格式的工作簿 细胞。

【讨论】:

  • 我查看了硬编码后生成的Excel文件,它是一个xlsx文件。此外,我的样本数据中只有 9 行,所以我想这不是我的问题的答案。不过,感谢您的帮助!
【解决方案4】:

试试这个代码

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]);
        }
     }

【讨论】:

    【解决方案5】:

    当我在 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2018-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多