8

I have been using the Microsoft.Office.Interop.Excel library to open Excel, refresh some queries and save. The issue I am running into is this will only work if each computer has the same Excel library as selected in the project installed on the PC.

I see that NPOI can http://npoi.codeplex.com/documentation read and write data to Excel, but what about an even simpler task of open/refresh/save, can NPOI handle this?

If you use this syntax it seems I can open my Excel file, but what about refreshing queries and saving?

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

private void button1_Click(object sender, EventArgs e)
{
    HSSFWorkbook hssfwb;
    using (FileStream file = new FileStream(@"c:\test.xls", FileMode.Open, FileAccess.Read))
    {
      hssfwb= new HSSFWorkbook(file);
    }
1

2 Answers 2

1

You can use OLEDB adapter also

System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = " + excelfilepath+ "; Extended Properties = \"Excel 8.0;HDR=NO;IMEX=1\";"); /*for office 2007 connection*/
conn.Open();
   string strQuery = "SELECT * FROM [" + Table + "]";
   System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(strQuery, conn);
 System.Data.DataTable ExcelToDataTable = new System.Data.DataTable();
 adapter.Fill(ExcelToDataTable);
0

By 'refresh queries' I assume you mean that the Excel file is linked to some data source, and you want to update the content of the Excel file with the current data from that data source.

If you want that to be "automatic" (i.e. you don't write the code to do this updating yourself), then I think Excel is the only way to go. I'm pretty sure that something like NPOI (or EPPlus, or ClosedXML) would not do that. However, you might be able to use those libraries to insert values into the spreadsheet yourself, if you can easily query the data source.

Since you're talking about multiple computers, I assume that you're building either an add-in or an EXE that gets deployed to various users. In that case, you probably want to build against the lowest-supported version of Excel that you can. In other words, you want (say) Excel 2003 or 2007 on your development machine. (You'll make your life much easier if you can drop support for 2003).

If you build against Excel 200x, then it should run fine on later versions. That's not the case the other way around. Note that you don't want to have more than one version on your development machine.

6
  • With the COM references, you can only have 1 reference installed at a time. So I have been unable to find a way to build for 2003 and have it open 2007 or vice-versa. Hence me looking into one of the other .dll's possible. Commented May 21, 2015 at 13:00
  • Yes, but COM components tend to be backward-compatible, so that if you've referenced version 1.x and the user has version 2.x, it'll still work. Commented May 21, 2015 at 13:38
  • Not in my experience. For example, if I use the Microsoft Excel 12.0 Object Library and instantiate Excel like so, if the user does not have Excel 2007 installed it will error out the syntax every time. public static Excel.Application oXL; oXL = new Excel.Application(); oXL.Visible = true; oXL.DisplayAlerts = false; oXL.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityLow; Commented May 21, 2015 at 19:13
  • It should work. How are you deploying your EXE? You would need to ensure that the Primary Interop Assembly from the version of Excel you build with is copied to the deployment folder, rather than relying on a newer version of the PIA in the GAC. Commented May 22, 2015 at 8:26
  • I was using a windows form. But did not ensure the PIA from my version of Excel was copied to the deployment folder. How would I go about that? Commented May 22, 2015 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.