保存 Excel 2007 文档

发布于 2024-07-30 10:43:30 字数 234 浏览 10 评论 0原文

在 .NET C# 中,我尝试打开 Excel 模板,添加一些数据并将其另存为新文档。 我正在尝试使用 OpenXML 文档格式。 我似乎找不到任何关于如何执行此操作的指导。 似乎所有文档都讨论了如何将各个部分写入包中,但我找不到任何有关完成并想要保存它时要做什么的信息。

有人知道我在哪里可以找到这些信息吗? 我一定是错误地思考了这个问题,因为我在看似非常基本的东西上找不到任何有用的东西。

谢谢

In .NET C# I'm trying to open an Excel template, add some data and save it as a new document. I'm trying to use the OpenXML document format. I can't seem to find any guidance on how to do this. Seems like all the documentation talks about how to write various parts to the Package but I can't find anything on what to do when you're done and want to save it.

Anyone know where I can find this information? I must be thinking about this incorrectly because I'm not finding anything useful on what seems to be very basic.

Thanks

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

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

发布评论

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

评论(2

£噩梦荏苒 2024-08-06 10:43:30

ExcelPackage 对此效果非常好。 我认为主要作者还没有对它进行过一段时间的开发,但它在其论坛上有很多追随者,可以解决任何问题。

            FileInfo template = new FileInfo(Path.GetDirectoryName(Application.ExecutablePath)+"\\Template.xlsx");
        try
        {
            using (ExcelPackage xlPackage = new ExcelPackage(strFileName,template))
            {
                //Enable DEBUG mode to create the xl folder (equlivant to expanding a xlsx.zip file)
                //xlPackage.DebugMode = true;

                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];

                worksheet.Name = WorkSheetName;

                foreach (DataRow row in dt.Rows)
                {
                    int c = 1;
                    if (r > startRow) worksheet.InsertRow(r);
                    // our query has the columns in the right order, so simply
                    // iterate through the columns
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (row[col].ToString() != null)
                        {
                            worksheet.Cell(r, c).Value = colValue;
                            worksheet.Column(c).Width = 10;
                        }
                        c++;
                    }
                    r++;
                }

                // change the sheet view to show it in page layout mode
                worksheet.View.PageLayoutView = false;

                // save our new workbook and we are done!
                xlPackage.Save();
                xlPackage.Dispose();
            }
        }

ExcelPackage works pretty good for that. It hasn't been worked on by the primary author I dont think for a little while but it has a good following of people on its forum that work any issues out.

            FileInfo template = new FileInfo(Path.GetDirectoryName(Application.ExecutablePath)+"\\Template.xlsx");
        try
        {
            using (ExcelPackage xlPackage = new ExcelPackage(strFileName,template))
            {
                //Enable DEBUG mode to create the xl folder (equlivant to expanding a xlsx.zip file)
                //xlPackage.DebugMode = true;

                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets["Sheet1"];

                worksheet.Name = WorkSheetName;

                foreach (DataRow row in dt.Rows)
                {
                    int c = 1;
                    if (r > startRow) worksheet.InsertRow(r);
                    // our query has the columns in the right order, so simply
                    // iterate through the columns
                    foreach (DataColumn col in dt.Columns)
                    {
                        if (row[col].ToString() != null)
                        {
                            worksheet.Cell(r, c).Value = colValue;
                            worksheet.Column(c).Width = 10;
                        }
                        c++;
                    }
                    r++;
                }

                // change the sheet view to show it in page layout mode
                worksheet.View.PageLayoutView = false;

                // save our new workbook and we are done!
                xlPackage.Save();
                xlPackage.Dispose();
            }
        }
复古式 2024-08-06 10:43:30

访问 Open XML / SpreadsheetML 文档绝非易事。 规格庞大且复杂。 “Open XML SDK”(谷歌一下)肯定有帮助,但仍然需要一些 Open XML 标准的知识才能完成很多工作。

SpreadsheetGear for .NET 具有类似于 Excel 的 API,可以读取和写入 Excel Open XML (xlsx) 文档,如下所示以及 Excel 97-2003 (xls) 文档。

您可以在此处查看一些 SpreadsheetGear 示例,并下载免费试用版此处

免责声明:我拥有 SpreadsheetGear LLC

Accessing Open XML / SpreadsheetML documents is far from a trivial exercise. The specification is large and complex. The "Open XML SDK" (google it) definitely helps, but still requires some knowledge of the Open XML standard to get much done.

SpreadsheetGear for .NET has an API similar to Excel and can read and write Excel Open XML (xlsx) documents as well as Excel 97-2003 (xls) documents.

You can see some SpreadsheetGear samples here and download a free trial here.

Disclaimer: I own SpreadsheetGear LLC

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