使用格式化模板生成 Excel 2003 电子表格的 .NET 组件

发布于 2024-08-04 04:59:19 字数 257 浏览 1 评论 0原文

我的应用程序试图解决的问题是:

当前,运行多个存储过程,将数据从 SQL 结果手动复制到空白电子表格中,并报告分析师格式化数据。这个过程被认为太长而且成本太高。

理想的解决方案(在我看来)是一个带有组件的 .NET 应用程序,该组件可以获取存储过程结果并将其写入工作表模板中的特定单元格范围(维护模板格式),然后我可以将结果整理到工作簿中在 .NET 应用程序中。

有这样的应用程序吗?更好的是,有人有更好的解决方案吗?

感谢您的考虑。

The problem my application is trying to solve is this:

Currently, several stored procedures are run, the data is manually copied out of the SQL results into a blank spreadsheet, and reporting analysts format the data. This process has been deemed too long and costly.

The ideal solution (in my mind) is a .NET application with a component that can take a stored procedure result and write it to a specific cell range in a worksheet template (maintaining template formatting) and then I can just collate the results into workbooks in the .NET application.

Are there any such applications out there? Even better, does anyone have any better solutions?

Thanks for your consideration.

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

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

发布评论

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

评论(4

丿*梦醉红颜 2024-08-11 04:59:19

我用来设置数据的

方式是获取数据,

object[,] excelSpreadsheetData

然后通过设置应用数据。

public void SetWorksheetData(long rowCount, long columnCount, object[,] excelSpreadsheetData, int startingRow, int startingCol, Worksheet worksheet)
        {
            if (rowCount == 0 || columnCount == 0) return;
            //set the region data.
            object m_objOpt = Missing.Value;
            Range cellRange = worksheet.get_Range(ExcelGeneratorUtils.ExcelColumnFromNumber(startingCol) + startingRow, m_objOpt);
            cellRange = cellRange.get_Resize(rowCount, columnCount);
            cellRange.set_Value(m_objOpt, excelSpreadsheetData);
        }

这应该保留模板格式,并一次性设置数据,比逐个单元格设置数据要快得多。

希望有帮助

What I use to set the data

get the data in the form of

object[,] excelSpreadsheetData

then apply the data by setting

public void SetWorksheetData(long rowCount, long columnCount, object[,] excelSpreadsheetData, int startingRow, int startingCol, Worksheet worksheet)
        {
            if (rowCount == 0 || columnCount == 0) return;
            //set the region data.
            object m_objOpt = Missing.Value;
            Range cellRange = worksheet.get_Range(ExcelGeneratorUtils.ExcelColumnFromNumber(startingCol) + startingRow, m_objOpt);
            cellRange = cellRange.get_Resize(rowCount, columnCount);
            cellRange.set_Value(m_objOpt, excelSpreadsheetData);
        }

This should keep the template formatting, and sets the data in one go, much faster than setting it cell, by cell.

Hope that helps

北笙凉宸 2024-08-11 04:59:19

SpreadsheetGear for .NET 可让您打开 Excel 工作簿、插入值并保存工作簿。

您可以在此处查看 Excel 报告(即导出到 Excel)示例并下载此处免费试用。

免责声明:我拥有 SpreadsheetGear LLC

SpreadsheetGear for .NET will let you open Excel workbooks, plug in values and save the workbook.

You can see Excel Reporting (i.e., export to Excel) samples here and download a free trial here.

Disclaimer: I own SpreadsheetGear LLC

若沐 2024-08-11 04:59:19

您始终可以通过互操作使用 Excel 自动化。当所需的输出是 Excel 时,我有时会这样做。它出奇地快,但您必须小心地自行清理,并且不要让 Excel 实例继续运行。这是一个简短的示例。

        using Excel = Microsoft.Office.Interop.Excel;

        Excel.ApplicationClass _Excel;
        Excel.Workbook WB;
        Excel.Worksheet WS;


        _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
        WB = _Excel.Workbooks.Add(System.Reflection.Missing.Value);

        WS = (Excel.Worksheet)WB.Worksheets[1]; 
        WS.Name = "Test";

         try
        {

            int row;
            int col;

            WS.Cells[++row, col] = "COL1";
            WS.Cells[row, ++col] = "COL2";

            WS.get_Range("A1", "A2").Font.Bold = true;
            WS.get_Range("A1", "A2").HorizontalAlignment                     =Excel.XlHAlign.xlHAlignCenter;

            WS.Cells[++row, col] = "Customer";
            WS.Cells[++row, col] = "Expenses" 

            WS.get_Range("A1", "B1").Font.Bold = true;


            WS.Columns.AutoFit();
            WS.Rows.AutoFit();

            WS.Activate();
           _Excel.Visible = true;
        }
        catch (Exception ex)
        {
            WB.Close(false, Type.Missing, Type.Missing);
            _Excel.Quit();
            throw;
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();



            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WS);


            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);


        }

You can always use Excel Automation through interop. I have done this on occasion when the desired output is Excel. Its surprisingly quick, you have to be careful though to clean up after yourself and not leave instances of Excel running. Here is a brief sample.

        using Excel = Microsoft.Office.Interop.Excel;

        Excel.ApplicationClass _Excel;
        Excel.Workbook WB;
        Excel.Worksheet WS;


        _Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
        WB = _Excel.Workbooks.Add(System.Reflection.Missing.Value);

        WS = (Excel.Worksheet)WB.Worksheets[1]; 
        WS.Name = "Test";

         try
        {

            int row;
            int col;

            WS.Cells[++row, col] = "COL1";
            WS.Cells[row, ++col] = "COL2";

            WS.get_Range("A1", "A2").Font.Bold = true;
            WS.get_Range("A1", "A2").HorizontalAlignment                     =Excel.XlHAlign.xlHAlignCenter;

            WS.Cells[++row, col] = "Customer";
            WS.Cells[++row, col] = "Expenses" 

            WS.get_Range("A1", "B1").Font.Bold = true;


            WS.Columns.AutoFit();
            WS.Rows.AutoFit();

            WS.Activate();
           _Excel.Visible = true;
        }
        catch (Exception ex)
        {
            WB.Close(false, Type.Missing, Type.Missing);
            _Excel.Quit();
            throw;
        }
        finally
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();



            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WS);


            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);

            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);


        }
随心而道 2024-08-11 04:59:19

这个问题中,我要求了类似的东西。不同之处在于我想要生成一个 XML 文件,该文件由 Excel 解释为电子表格。如果您可以将数据发送到 XML 文件,然后将 XML 转换为 SpreadsheetML,那么这可能会解决您的问题。
不过,SpreadsheetXM 确实有一些限制。它不需要安装 Excel,而且转换速度非常快。如果您使用 Excel 2007,则可以使用第二种 XML 格式,即 Microsoft Office XML 格式。
基本上,它很慢,因为我怀疑您当前的代码正在通过 COM 与 Excel 交互。

In this question I asked for something similar. The difference is that I want something to generate an XML file that's interpreted by Excel as a spreadsheet. If you could send the data to an XML file, then transform the XML to SpreadsheetML, then this might solve your problem.
SpreadsheetXM does have a few limitations, though. It won't require Excel to be installed and the transformation can be real fast. And if you use Excel 2007 then there's a second XML format that you could use, the Microsoft Office XML format.
Basically, it's slow because I suspect that your current code is interacting with Excel through COM.

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