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);
}
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With