如何将datagridview导出到Open Office Excel c#
Posted
技术标签:
【中文标题】如何将datagridview导出到Open Office Excel c#【英文标题】:How to export datagridview into Open Office Excel c# 【发布时间】:2014-12-17 07:20:27 【问题描述】:我有一个 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。
【问题讨论】:
此文件应该适用于 Open Office。使用 Open Office 打开时出现什么错误? 【参考方案1】:您需要为以下汇编添加引用: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;
【讨论】:
谢谢@Gleb。我得到了它。但我有一个关于程序集的问题。如何添加它们?以上是关于如何将datagridview导出到Open Office Excel c#的主要内容,如果未能解决你的问题,请参考以下文章
如何将.net中DataGridView中的数据导出到Excel文本(是在windows窗体中)