You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by bu...@apache.org on 2021/11/16 08:39:46 UTC
[Bug 65683] New: call workbook.removeSheetAt do won`t remove attachments
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
Bug ID: 65683
Summary: call workbook.removeSheetAt do won`t remove
attachments
Product: POI
Version: 4.1.2-FINAL
Hardware: PC
Status: NEW
Severity: normal
Priority: P2
Component: XSSF
Assignee: dev@poi.apache.org
Reporter: 1120955357@qq.com
Target Milestone: ---
I have an Excel file that contains several sheet pages, each containing one or
more attachments, and I want to extract one of the sheet pages and save it to a
new workbook.
My approach is to remove the rest of the sheet pages from the original document
and save them to a new file. This will achieve what I need, but the extracted
file is basically the same size as the original file, and I want to be able to
keep only the files in the sheet I need in the new workbook.
Can anyone help me?
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt do won`t remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
--- Comment #3 from 1120955357@qq.com ---
Created attachment 38096
--> https://bz.apache.org/bugzilla/attachment.cgi?id=38096&action=edit
this is extract whole sheet named "attachment1" file from the origin excel
this is test case.
```
@Test
public void testExtractSheet() throws IOException {
Workbook workbook = WorkbookFactory.create(new File("template -
副本.xls"));
try {
int numberOfSheets = workbook.getNumberOfSheets();
boolean found = false;
String sheetNameToExtract = "attachment1";
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheetAt = workbook.getSheetAt(i);
if
(!sheetAt.getSheetName().equalsIgnoreCase(sheetNameToExtract)) {
workbook.removeSheetAt(i--);
numberOfSheets--;
} else {
found = true;
}
}
if (!found) {
workbook.close();
throw new FileNotFoundException("can not find sheet: " +
sheetNameToExtract);
}
File outputFile = new File(System.currentTimeMillis() + ".xls");
FileUtils.createParentDirectories(outputFile);
try (FileOutputStream stream = new FileOutputStream(outputFile)) {
workbook.write(stream);
}
} finally {
org.apache.commons.io.IOUtils.closeQuietly(workbook);
}
}
```
The result is that the original excel file is 847K and the sheet named
"attachment1" contains only a simple excel, but when it is extracted to a new
file, the new file size is 845K, so I guess poi will not delete the irrelevant
files from this sheet.
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt do won`t remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
Dominik Stadler <do...@gmx.at> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEEDINFO |NEW
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt do won`t remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
--- Comment #2 from 1120955357@qq.com ---
Created attachment 38095
--> https://bz.apache.org/bugzilla/attachment.cgi?id=38095&action=edit
this is origin excel template
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt do won`t remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
1120955357@qq.com changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |1120955357@qq.com
OS| |All
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt will not remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
--- Comment #4 from PJ Fanning <fa...@yahoo.com> ---
Maybe you could write custom code to remove the attachments yourself.
https://github.com/apache/poi/blob/trunk/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java
will give you an idea how to iterate over the attachments.
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt do won`t remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
Dominik Stadler <do...@gmx.at> changed:
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |NEEDINFO
--- Comment #1 from Dominik Stadler <do...@gmx.at> ---
Can you attach a sample workbook and some code for a reproducible test-case
which shows your case and allows others to try to help?
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org
[Bug 65683] call workbook.removeSheetAt will not remove attachments
Posted by bu...@apache.org.
https://bz.apache.org/bugzilla/show_bug.cgi?id=65683
1120955357@qq.com changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|call workbook.removeSheetAt |call workbook.removeSheetAt
|do won`t remove attachments |will not remove attachments
--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org
For additional commands, e-mail: dev-help@poi.apache.org