如何以编程方式从 Excel 工作表模板创建 Excel 工作表?

发布于 2024-08-09 10:45:31 字数 806 浏览 3 评论 0原文

我正在从模板 Excel 工作表创建 Excel 工作表。

我有一个代码,

try
{
    FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
    FileInfo template = new FileInfo(@"D:\template.xlsx");

    using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
    {
        
        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
        
        ExcelCell cell = worksheet.Cell(5,1);
        cell.Value = "15";
        
        //worksheet.Cell(5, 1).Value = "Soap";

        //xlPackage.Save();
        Response.Write("Excel file created successfully");
    }

}
catch (Exception ex)
{
    Response.WriteFile(ex.InnerException.ToString());
}  

此代码创建与模板 Excel 文件相同的新 Excel 文件,但无法添加单元格值。为什么?

我已经尝试了两种方法,如上面的单元格(5,1)代码。但是Excel工作表创建时没有写入单元格值。我们如何添加它。

I am creating a excel sheet from tempalte excel sheet.

I have a code

try
{
    FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
    FileInfo template = new FileInfo(@"D:\template.xlsx");

    using (ExcelPackage xlPackage = new ExcelPackage(newFile, template))
    {
        
        ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];
        
        ExcelCell cell = worksheet.Cell(5,1);
        cell.Value = "15";
        
        //worksheet.Cell(5, 1).Value = "Soap";

        //xlPackage.Save();
        Response.Write("Excel file created successfully");
    }

}
catch (Exception ex)
{
    Response.WriteFile(ex.InnerException.ToString());
}  

this code creates the new excel file same as the template excel file but could not add the cell value. Why?

I had tried it with 2 ways as in above code for cell(5,1). But the excel sheet creates without writting cell value. How we can add it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-08-16 10:45:32

您需要保存文件以保留所做的更改。使用 save()

  try
        {
            FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
            FileInfo template = new FileInfo(@"C:\Example.xlsx");

            using (ExcelPackage xlPackage = new ExcelPackage(newFile , template))
            {

               //Added This part
               foreach (ExcelWorksheet aworksheet in xlPackage.Workbook.Worksheets)
                {
                    aworksheet.Cell(1, 1).Value = aworksheet.Cell(1, 1).Value;
                }

                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["My Data"];

                ExcelCell cell = worksheet.Cell(5, 1);
                cell.Value = "15";

                //worksheet.Cell(5, 1).Value = "Soap";

                xlPackage.Save( );
                //Response.Write("Excel file created successfully");
            }

        }
        catch (Exception ex)
        {
            //Response.WriteFile(ex.InnerException.ToString());
        }

遇到了问题。该问题是 ExcelPackage 固有的。
同样,您必须打开每个工作表并进行一些更改才能保存。

搜索论坛以获取更多解释。

You need to save the file to persist the changes made. Using save()

  try
        {
            FileInfo newFile = new FileInfo(@"D:\ExcelFromTemplate.xlsx");
            FileInfo template = new FileInfo(@"C:\Example.xlsx");

            using (ExcelPackage xlPackage = new ExcelPackage(newFile , template))
            {

               //Added This part
               foreach (ExcelWorksheet aworksheet in xlPackage.Workbook.Worksheets)
                {
                    aworksheet.Cell(1, 1).Value = aworksheet.Cell(1, 1).Value;
                }

                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["My Data"];

                ExcelCell cell = worksheet.Cell(5, 1);
                cell.Value = "15";

                //worksheet.Cell(5, 1).Value = "Soap";

                xlPackage.Save( );
                //Response.Write("Excel file created successfully");
            }

        }
        catch (Exception ex)
        {
            //Response.WriteFile(ex.InnerException.ToString());
        }

Got the issue. The problem is inherent to ExcelPackage.
For the same you have to open each sheet and do some changes for it to get saved.

Search the forum for more explanation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文