views:

775

answers:

8

On this project I am working on right now, one of the newest feature requests is to output expense information which is stored into expense report that matches an Excel worksheet they already use for all of their expense reporting.

I was curious if there was a way that I could take this excel worksheet (with all of its layout already done) and just fill in the parts that our system tracks and then serve it up as an .xls file to the user without having to do Excel Automation or any other method that requires Excel to be installed on the server.

Note: development environment is ASP.NET 2.0

A: 

Try Aspose.Cells - it works great for this.

Ian Varley
would aspose make it possible to work with an excel file that has already been created, or would one have to go and create all the layout for the end-report in the back-end instead of working from an excel file already there?
TheTXI
A: 

Telerik RAD Grid for Asp.NetAJAX has nice Excel export.

dmajkic
+1  A: 

You can try Spire.XLS ( http://www.e-iceblue.com/xls/xlsintro.htm )

It's not free, but does a great job of reading and creating XLS documents without having to have Excel present on the server.

nsr81
+3  A: 

I am pretty sure you can can use OleDB to open the excel file(with the template)

Example 1

string ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=C:\Test Projects\Excel example\Excel - reading an excel file\ReadThis.xls;
        Extended Properties=Excel 5.0";

//Create the connection
 System.Data.OleDb.OleDbConnection ExcelConnection = new System.Data.OleDb.OleDbConnection(ConnectionString);

Link to article 1

Example 2

string filename = @"C:\testdata.xls";

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=" + filename + ";" +

"Extended Properties=Excel 8.0;";

OleDbConnection objConn = new OleDbConnection(connectionString);

objConn.Open();

OleDbCommand ObjCommand = new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdp = new OleDbDataAdapter();

objAdp.SelectCommand = ObjCommand;

DataSet myDataSet = new DataSet();

objAdp.Fill(myDataSet);

DataTable dataTable = myDataSet.Tables["Sheet1$"];

Link to Article 2

Once you have opened the connection you should be able to read and write to it however you like!

cgreeno
+1  A: 

One of the ways that I have done this is using Office XML to generate the documents. Since you have a specific format that the document needs to be in, things might be a bit harder, but the way I have done things before in the past is to export the data that needs to be displayed on the report to an XML file (either saved on disk or in the memory) and then apply an XSL transform to convert things over to the correctly formatted document that Excel can load. For reference, here are some links for you:

Rob
A: 

Another two solutions are:

  • use OpenOffice (they have a mode where no UI is loaded/shown)
  • use the FileHelpers library (you can read/write data to an excel file)

I must say I don't have experience with either of them... but they sound like solid alternatives beside the JET.OLEDB provider and the Xml-to-Excel through Xsl (which I both have used in the past) mentioned by Chris and Rob.

Just wanted to help you on your way and mention some alternatives (I don't expect much points :) )

Cohen
A: 

Although this won't process your existing spreadsheets, a really simple and low-tech way to generate Excel files is to create a html file with the correct content-type and use the .xls extension. When Excel (on the client) opens the file, it is treated as if it was a native spreadsheet.

Example:

<html>
    <head>
     <meta http-equiv="Content-Type" content="application/vnd.ms-excel" />
     <title></title>
    </head>
    <body>
     <table>
      <tr>
       <td><b>Forename</b></td>
       <td><b>Gender</b></td>
      </tr>
      <tr>
       <td>Simon</td>
       <td>Male</td>
      </tr>
      <tr>
       <td>Katy</td>
       <td>Female</td>
      </tr>
     </table>
    </body>
</html>

Caveats: Of course you would not include all the indentation and spacing in the real file! I am using Excel 2003.

Another small bonus of this technique is that these files are very efficient to generate.

Downsides: I've not found a way to turn html into multiple Worksheets, or to do anything particularly technical.

A: 

I have tried Aspose.Cells for .NET, it works fine for both scenarios whether you are using an existing file (fully formatted template excel file) or creating the formatted excel file from the scratch. The performance is great comparing it with office automation.

I think for your scenario you may do as follows. Consider you have a "File1.xls" file (fully formatted excel file). Now if you want to enter your data into some range of cells, you can simply open/load the file and then fill into your desired cells, finally save or save as the file. The file would be generated with all the existing formatting retained in it.

Workbook workbook = new Workbook(); FileStream fstream = new FileStream("C:\File1.xls", FileMode.Open); workbook.Open(fstream);

Cell cell = workbook.Worksheets[0].Cells["A1"]; //Fill data into your desired cells e.g cell.PutValue("Hello World!");

//Save as the file. workbook.Save("C:\File2.xls");

fstream.Close();

qamarhash2