Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quickest Way To Open Excel

Tags:

c#

excel

npoi

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);
    }
like image 326
MasterOfStupidQuestions Avatar asked Apr 25 '15 00:04

MasterOfStupidQuestions


1 Answers

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);
like image 162
neha kochhar Avatar answered Sep 20 '22 13:09

neha kochhar