Bug 43949 - corrupted damaged xls file (corrupted, excel repairs it during opening)
Summary: corrupted damaged xls file (corrupted, excel repairs it during opening)
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-FINAL
Hardware: Other other
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-11-23 07:17 UTC by Nail A.
Modified: 2007-11-23 07:57 UTC (History)
0 users



Attachments
corrupted damaged XLS file (4.34 KB, application/vnd.ms-excel)
2007-11-23 07:19 UTC, Nail A.
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Nail A. 2007-11-23 07:17:43 UTC
Hello.

My POI-Version: poi-bin-3.0.1-FINAL-20070705.zip

I've generated XLS-File with:

		try {
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFCellStyle styleDate = wb.createCellStyle();
			styleDate.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));

			if (result != null) {
				// create a new sheet
				HSSFSheet s = wb.createSheet();

				// declare a row object reference
				HSSFRow r = null;

				// declare a cell object reference
				HSSFCell c = null;

				wb.setSheetName(0, "RemovalInstallationList");

				// first row with headings
				r = s.createRow(0);

				c = r.createCell((short) 0);
				c.setCellValue(new HSSFRichTextString("Three Letter Code"));
			
				....

				c = r.createCell((short) 37);
				c.setCellValue(new HSSFRichTextString("Description"));
				
				s.setColumnWidth((short) (0), (short) ((50 * 4) / ((double) 1 / 20)));
			
				....

				s.setColumnWidth((short) (37), (short) ((50 * 4) / ((double) 1 / 20)));
				
				Iterator it = result.iterator();
				for (int i = 0; it.hasNext(); i++) {
					RemovalInstallationReportResultRowModel resultModel = null;
					resultModel = (RemovalInstallationReportResultRowModel) it.next();

					// create a row with data
					r = s.createRow(i + 1);

					c = r.createCell((short) 0);
					if (resultModel.getThreeLC() != null) {
						c.setCellValue(new HSSFRichTextString(resultModel.getThreeLC()));
					} else {
						c.setCellValue(new HSSFRichTextString(""));
					}
			
					....

					c = r.createCell((short) 37);
					if (resultModel.getDescriptionPartNoInstalledComponent() != null) {
						c.setCellValue(new
HSSFRichTextString(resultModel.getDescriptionPartNoInstalledComponent()));
					} else {
						c.setCellValue(new HSSFRichTextString(""));
					}
				}
				return new RemovalInstallationReportCSVModel(Status.OK, wb.getBytes());
			}
			
			calling method:
			
			byte[] resultAsByte = csvModel.getTransferObject();
			if (result != null) {
			inputStream = new ByteArrayInputStream(resultAsByte);
		}
		input();
		return "exportCSV";		


    Struts-Config.xml

		<result name="exportCSV" type="stream">
			  <param name="contentType">application/vnd.ms-excel</param>
			  <param name="inputName">inputStream</param>
			  <param name="contentDisposition">filename="document.xls"</param>
		</result>
		
		The result is a message from Excel during opening the file like:
		
		Microsoft Excel Datei-Reparatur Protokoll

		In Datei 'D:\tmp\exported_file_damaged_231107.xls' wurden Fehler festgestellt
		Die folgenden Reparaturen wurden durchgeführt:
		Die Zusammenfassungs-Information des Dokumentes ist verloren gegangen.

The resulting exported file is attached.

Kind regards.
Comment 1 Nail A. 2007-11-23 07:19:16 UTC
Created attachment 21177 [details]
corrupted damaged XLS file
Comment 2 Yegor Kozlov 2007-11-23 07:48:17 UTC
You are not supposed to call workbook.getBytes() directly. This method is used
to construct the Workbook node in the document file system. 

Change the following line
return new RemovalInstallationReportCSVModel(Status.OK, wb.getBytes());

to

ByteArrayOutputStream out = new ByteArrayOutputStream();
wb.write(out); 
return new RemovalInstallationReportCSVModel(Status.OK, out.toByteArray());

It should work.

Yegor 
Comment 3 Nail A. 2007-11-23 07:57:40 UTC
@Yegor: Thank you very much, it work's now. Best regards & a nice weekend.