简体   繁体   English

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

[英].NET Component for Excel 2003 spreadsheet generation using templates for formatting

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. 当前,正在运行几个存储过程,将数据从SQL结果中手动复制到空白电子表格中,然后报表分析人员将数据格式化。 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. 理想的解决方案(在我看来)是带有组件的.NET应用程序,该组件可以获取存储过程结果并将其写入工作表模板中的特定单元格区域(保持模板格式),然后我可以将结果整理到工作簿中在.NET应用程序中。

Are there any such applications out there? 那里有这样的应用程序吗? Even better, does anyone have any better solutions? 更好的是,有人有更好的解决方案吗?

Thanks for your consideration. 感谢您的考虑。

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 希望能有所帮助

You can always use Excel Automation through interop. 您始终可以通过互操作使用Excel Automation。 I have done this on occasion when the desired output is Excel. 当所需的输出是Excel时,我有时会这样做。 Its surprisingly quick, you have to be careful though to clean up after yourself and not leave instances of Excel running. 它出奇地快,您必须谨慎,但要自己清理,而不要让Excel实例运行。 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);


        }

SpreadsheetGear for .NET will let you open Excel workbooks, plug in values and save the workbook. 使用SpreadsheetGear for .NET,您可以打开Excel工作簿,插入值并保存工作簿。

You can see Excel Reporting (ie, export to Excel) samples here and download a free trial here . 你可以看到Excel的报表(即导出到Excel)样品这里并下载免费试用这里

Disclaimer: I own SpreadsheetGear LLC 免责声明:我拥有SpreadsheetGear LLC

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. 所不同的是,我想要一些东西来生成XML文件,该文件被Excel解释为电子表格。 If you could send the data to an XML file, then transform the XML to SpreadsheetML, then this might solve your problem. 如果可以将数据发送到XML文件,然后将XML转换为SpreadsheetML,则可以解决您的问题。 SpreadsheetXM does have a few limitations, though. 但是SpreadsheetXM确实有一些限制。 It won't require Excel to be installed and the transformation can be real fast. 它不需要安装Excel,并且转换可以非常快速。 And if you use Excel 2007 then there's a second XML format that you could use, the Microsoft Office XML format. 如果使用Excel 2007,则可以使用第二种XML格式,即Microsoft Office XML格式。 Basically, it's slow because I suspect that your current code is interacting with Excel through COM. 基本上,它很慢,因为我怀疑您当前的代码正在通过COM与Excel交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM