C# 如何将datagridview导出到OpenOfficeExcelC中#
我有一个datagridview,可以将其导出到Microsoft Excel。现在我想导出datagridview以打开Office Excel。我无法确定我的Open Office项目将引用哪些程序集。对于Microsoft Excel,我使用了以下引用,后跟事件代码C# 如何将datagridview导出到OpenOfficeExcelC中#,c#,datagridview,interop,.net-assembly,openoffice.org,C#,Datagridview,Interop,.net Assembly,Openoffice.org,我有一个datagridview,可以将其导出到Microsoft Excel。现在我想导出datagridview以打开Office Excel。我无法确定我的Open Office项目将引用哪些程序集。对于Microsoft Excel,我使用了以下引用,后跟事件代码 using Excel=Microsoft.Office.Interop.Excel; private void exportToMSExcelToolStripMenuItem_Click(object sender,
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
{
}
}
使用Excel=Microsoft.Office.Interop.Excel;
私有void exportToMSExcelToolStripMenuItem\u单击(对象发送方,事件参数e)
{
尝试
{
Microsoft.Office.Interop.Excel.\u应用程序app=新的Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.\u工作簿=app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel.\u工作表=null;
app.Visible=true;
工作表=工作簿。工作表[“Sheet1”];
工作表=工作簿.ActiveSheet;
工作表.Name=“从.net应用程序导出”;
对于(int i=1;i”+ex.Message.ToString(),“ERROR”);
}
最后
{
}
}
这对于Microsoft Office很好,但现在我想将其替换为Open Office Excel。您需要为以下程序集添加引用:cli\u basetype、cli\u cppihelper、cli\u OO类型、cli\u uretypes、cli\u 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;
此文件应适用于Open Office。当您使用open Office打开它时,会出现什么错误?谢谢@Gleb。我得到了它。但是我有一个关于集会的问题。如何添加它们?