New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
writing html file content to second sheet of the workbook #1823
Comments
Rather than setting the active sheet in the spreadsheet, you need to set it in the reader: $reader->setSheetIndex(1);
$reader->loadFromString($html, $spreadsheet); |
That problem is solved. Thank you, but i have another one if you do not mind to answer. I have two html files named x and z. I have to write X data to sheet 1 and 'Z' file data to sheet 2 simultaneously. But its not working at the moment. Using the following code at the moment
Do tell us as how we do this. |
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->load('xcomment.html');
$spreadsheet->getSheet(0)->setTitle('file1');
$reader->setSheetIndex(1);
$reader->loadIntoExisting('xcomment2.html', $spreadsheet);
$writer1 = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer1->save('outfile.xlsx'); Note the setTitle statement. The html load routines don't assign a title to the sheet, so they get a default name, and, if you try to save a spreadsheet with 2 sheets with the same name, Excel will not be able to open it properly. I think that PhpSpreadsheet might be able to do better here. I'll think about how to address that; in the meantime, the code above should work. |
Thanks, it is working. |
The title for the worksheet is, understandably, set using the html title tag (or a default value if there is no such tag). My 2 files happened to use the same title tag, which is why both worksheets wound up with the same title. So, setTitle is needed only if the titles conflict, and I don't think PhpSpreadsheet requires any changes. |
This issue arose while researching issue #1823. The issue was not a bug; it just required clarification to the author of how to use the software. But, while researching, I discovered that loading html into 2 sheets of a spreadsheet has a problem if the html title tag is the same for the 2 sheets. PhpSpreadsheet would be able to save the resulting file, but Excel would not be able to read it properly because of the duplicate title. The worksheet setTitle method allows for disambiguation is such a circumstance. The html reader passed a parameter indicating "don't disambiguate", but I can't see any harm in changing that to "disambiguate". An extremely simple fix, with tests to back it up.
Closing issue - author's questions are answered, and code has been made a bit more resilient. |
This is: a feature request
What is the expected behavior?
Need write data into the second sheet of the workbook created
What is the current behavior?
By default, it saves into the first sheet of the workbook
What are the steps to reproduce?
This is what i have tried until now:
Step 1: Loaded external html file to read
Step 2: Created new sheet
Step 3: Tried to set active sheet to 1, hoping that it would write data to that particular sheet
Step 4: Loaded the data into spreadsheet
Step 5: File saved, but data still in the first worksheet.
Here is the code i have used.
Which versions of PhpSpreadsheet and PHP are affected?
The latest one
The text was updated successfully, but these errors were encountered: