简体   繁体   中英

opening excel file via c# application

My application using excel file in order to read data from web (using excel web query and reading the data from the excel file)
The excel file I'm using called webGate.
my problem is that I don't want webGate to be presented to the user.
if the user opens excel file (any other file) while my application is reading the webGate file the webGate file is presented with the user file
(ie user opens excel file and two files are being presented one of them is the webGate)
Any idea about how can I use the file but it will never be presented to the user?
Thanks
Eran

What do you want to achieve? Its is unclear. Any idea about how can I use the file but it will never be presented to the user?

If you want to open/modify the file in C# code: http://www.codeproject.com/KB/office/csharp_excel.aspx

You can use an.xls as a OLEDB data source. You use the Microsoft.Jet.OLEDB.4.0 data provider.

This means you are reading the data out of the excel sheet, rather than opening the.xls as a document in the Excel application.

It is not automation. It does not start Excel.exe. It does not require Excel.exe. There is no third-party component required. There's no additional cost (beyond the cost of Windows). It works in server-side apps.

Full source code here .

Snippets:

void CreateExcelDocThroughAdoNet()
{
    const string Filename= "adonet-excel.xls";

    const string strConnect = 
      "Provider=Microsoft.Jet.OLEDB.4.0;" + 
      "Data Source=" + Filename + ";" + 
      "Extended Properties=\"Excel 8.0;HDR=yes;\""; 

    try
    {
      conn = new System.Data.OleDb.OleDbConnection(strConnect);
      CreateTable();
      for(int i=0; i < 4; i++) Insert(i);
    }
    catch (System.Exception ex)
    {
      System.Console.WriteLine("Exception: " + ex.Message+ "\n  " + ex.StackTrace); 
    }
}


private void CreateTable()
{

    string strSql = "CREATE TABLE SampleTable ( Ix NUMBER, CustName char(255), Stamp datetime )";

    System.Data.OleDb.OleDbCommand cmd= new System.Data.OleDb.OleDbCommand(strSql, conn);
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}

private void Insert(int ix)
{
    string strSql = "insert into [SampleTable] ([ix],[CustName],[Stamp]) value(@p1,@p2,@p3)";

    System.Data.OleDb.OleDbCommand cmd= new System.Data.OleDb.OleDbCommand(strSql, conn);

    cmd.Parameters.Add("@p1", System.Data.OleDb.OleDbType.Numeric).Value = ix;
    cmd.Parameters.Add("@p2", System.Data.OleDb.OleDbType.VarChar).Value = "Some text";
    cmd.Parameters.Add("@p3", System.Data.OleDb.OleDbType.Date).Value = System.DateTime.Now;

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
  }

I don't know if this would be too much trouble or not, but it might be worth it to export the excel file as an xml (if this is possible, I don't know what your excel file looks like) and read the data in that way.

I use the Infragistics toolset for this purpose, and it works great (supports all excel versions, and is very feature rich)

It may not suit your needs because it's not free, but it's largely worth its cost if you ask me

If the project will take less than 30 days, you can use the free evaluation of SpreadsheetGear for .NET to embed an Excel workbook in a .NET application without it being presented to the user. Just follow these steps:

  • Create a new C# Windows Forms application and put a WorkbookView control on your form (see the SpreadsheetGear Tutorial for Windows Forms app if your not sure how to do this).
  • Right click on the WorkbookView and chose "Workbook Designer...".
  • In the Workbook Designer, close the default workbook and use File Open... to open your workbook. This will embed your workbook in the application as a resource.
  • Close the Workbook Designer.
  • You can hide the WorkbookView if you don't want the user to see it, or you can use the WorkbookView.DisplayReference property to limit what parts of the workbook they can see.

You can download the free evaluation here .

Disclaimer: I own SpreadsheetGear LLC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM