Asked  1 Year ago    Answers:  5   Viewed   17 times

Is it possible to append content to an .xls file using PHP fwrite()?

When I try this using fwrite(), the resulting file causes an error message in Excel 2007.

Is there a specific separator I can use to accomplish this?

Is it possible without a third party library?

 Answers

5

You can use the PhpSpreadsheet library, to read an existing Excel file, add new rows/columns to it, then write it back as a real Excel file.

Disclaimer: I am one of the authors of this library.

Thursday, April 1, 2021
 
mozlima
 
1

The only thing you really need to do is save the Excel contents to disk on your server, you can also remove those headers completely as they are only needed when outputting the data to the browser.

When you mail the user, simply attach the file and send.

It should be pretty straight forward.

EDIT:

To save the excel file:

file_put_contents('<your file name>', '<your file contents>');

The content can be any string, so if you have the excel file contents in a variable, simple put it there.

The file name can be a relative or absolute path.

Thursday, April 1, 2021
 
1

try below code on the button click

// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;

// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);

// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
Saturday, August 21, 2021
 
3

If you don't want to use a third party library or automation, and you need some kind of formatting (colors, fonts, etc...) you can simply create a open office xml. http://en.wikipedia.org/wiki/Microsoft_Office_2003_XML_formats

Thursday, October 21, 2021
 
danijar
 
3

There are three different methods I know of:

Custom XML parts For an application-level add in, this is my preferred method of storing any application data that needs to be persisted in a saved xls file without ever being visible to the user.

http://msdn.microsoft.com/en-us/library/bb608612.aspx

Cached Data Islands This only works for document-level add ins. You will get exceptions if you try to use it in an application-level add in.

http://blogs.msdn.com/b/eric_carter/archive/2004/04/23/119294.aspx

Hidden worksheet Using VSTO, you can create invisible worksheets that cannot be seen by users. This works well, but leads to a lot of awkward coding to convert your data to fit in an excel sheet.

Update (2014): So in the end the usage of Custom XML parts turned out to be a performance issue so my application had to be changed back to use hidden worksheets. Apparently, once the XML reaches a certain size, Excel becomes very sluggish. My add-in had Custom Parts reach thousands of nodes, and the larger the XML grew, the slower everything in Excel became. For example, simply clicking on any cell would incur a very noticeable delay.

Tuesday, October 26, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :