繁体   English   中英

如何将datagridview导出到Open Office Excel C#

[英]How to export datagridview into Open Office Excel c#

我有一个datagridview,我可以将其导出到Microsoft Excel。 现在,我想将datagridview导出到Open Office Excel。 我无法弄清楚在我的Open Office项目中将使用哪些程序集作为参考。 对于Microsoft Excel,我使用了以下参考以及事件代码。

using Excel=Microsoft.Office.Interop.Excel;


private void exportToMSExcelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {

                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._Worksheet worksheet = null;
                app.Visible = true;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                worksheet.Name = "Exported from .net app";
                for (int i = 1; i < datagridEC.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = datagridEC.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < datagridEC.Rows.Count; i++)
                {
                    for (int j = 0; j < datagridEC.Columns.Count; j++)
                    {
                        worksheet.Cells[i + 2, j + 1] = datagridEC.Rows[i].Cells[j].Value.ToString();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("ERROR--> " + ex.Message.ToString(), "ERROR");
            }
            finally
            {
            }
        }

这对于Microsoft Office正常工作,但现在我想将其替换为Open Office Excel。

您需要为以下汇编添加引用:cli_basetype,cli_cppihelper,cli_oootypes,cli_ure,cli_uretypes

并且

using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.bridge;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;
using UNO = unoidl.com.sun.star.uno;
using OOo = unoidl.com.sun.star;
using unoidl.com.sun.star.text;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.table;

以及如何获取工作表的示例:

            UNO.XComponentContext context = uno.util.Bootstrap.bootstrap();
            XMultiServiceFactory SrvManager = (XMultiServiceFactory)context.getServiceManager();
            XComponentLoader loader = (XComponentLoader)SrvManager.createInstance("com.sun.star.frame.Desktop");

            string AFile = "file:///" + "filePath";

            PropertyValue[] props = new PropertyValue[0];

            XComponent oDoc = loader.loadComponentFromURL(AFile, "_blank", 0, props);

            XSpreadsheetDocument xSpreadDoc = (XSpreadsheetDocument)oDoc;

            XSpreadsheets xSheets = xSpreadDoc.getSheets();
            XIndexAccess xIA = (XIndexAccess)xSheets;

            XSpreadsheet xSheet = (XSpreadsheet)xIA.getByIndex(0).Value;

暂无
暂无

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

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