Skip to content
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

Closed
husaindevelop opened this issue Feb 1, 2021 · 6 comments
Closed

writing html file content to second sheet of the workbook #1823

husaindevelop opened this issue Feb 1, 2021 · 6 comments

Comments

@husaindevelop
Copy link

This is: a feature request

- [ ] a bug report
- [ ] a feature request
- [ ] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

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.

<?php

require __DIR__ . '/vendor/autoload.php';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
$spreadsheet = $reader->load("filename.html");
$spreadsheet->createSheet();
$spreadsheet->setActiveSheetIndex(1);
$spreadsheet = $reader->loadFromString($html);

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('new.xlsx'); 

Which versions of PhpSpreadsheet and PHP are affected?

The latest one

@oleibman
Copy link
Collaborator

oleibman commented Feb 1, 2021

Rather than setting the active sheet in the spreadsheet, you need to set it in the reader:

$reader->setSheetIndex(1);
$reader->loadFromString($html, $spreadsheet);

@husaindevelop
Copy link
Author

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

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();

$spreadsheet1 = $reader->load("filename.html");
$spreadsheet1->createSheet();
$spreadsheet1 = $reader->setSheetIndex(0);
$spreadsheet1 = $reader->load("filename.html", $spreadsheet1);
$writer1 = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet1, 'Xlsx');
$writer1->save($_GET ['get'].'.xlsx'); 


$spreadsheet = $reader->load("filename.html");
$spreadsheet = $reader->setSheetIndex(1);
$spreadsheet = $reader->load("filename.html", $spreadsheet);
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($_GET ['get'].'.xlsx');  

Do tell us as how we do this.

@oleibman
Copy link
Collaborator

oleibman commented Feb 2, 2021

$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.

@husaindevelop
Copy link
Author

Thanks, it is working.

@oleibman
Copy link
Collaborator

oleibman commented Feb 2, 2021

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.

MarkBaker pushed a commit that referenced this issue Feb 27, 2021
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.
@oleibman
Copy link
Collaborator

Closing issue - author's questions are answered, and code has been made a bit more resilient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants