Bug 49677 - About sheet.getDefaultColumnWidth() serious error!
Summary: About sheet.getDefaultColumnWidth() serious error!
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: POI Overall (show other bugs)
Version: 3.6-FINAL
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-30 05:17 UTC by longxibo
Modified: 2011-06-20 17:09 UTC (History)
0 users



Attachments
For test (17.00 KB, application/octet-stream)
2010-07-30 05:17 UTC, longxibo
Details

Note You need to log in before you can comment on or make changes to this bug.
Description longxibo 2010-07-30 05:17:51 UTC
Created attachment 25821 [details]
For test

1.For HSSF
  //When I adjust xls default column width by excel 2007.then use follow code  
  //get defaultcolumn width ,broke a excepton
@Test
    public void testGetDefaultColumnWidthForHSSF() {
	HSSFWorkbook wb;
	HSSFSheet sheet;
	FileInputStream finput = null;
	File file;
	String xlsSourceFile = "testfile\\testcolumnwidth.xls";
	file = new File(xlsSourceFile);
	try {
	    finput = new FileInputStream(file);
	    wb = new HSSFWorkbook(finput);
	    finput.close();
	    finput = null;
	    sheet = wb.getSheetAt(2);
	    System.out.println(sheet.getDefaultColumnWidth());
	    
	} catch (FileNotFoundException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    try {
		if (finput != null)
		    finput.close();
		
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
  }
// Exception :
org.apache.poi.hssf.record.RecordFormatException: Unable to construct record instance
	at org.apache.poi.hssf.record.RecordFactory$ReflectionRecordCreator.create(RecordFactory.java:64)
	at org.apache.poi.hssf.record.RecordFactory.createSingleRecord(RecordFactory.java:263)
	at org.apache.poi.hssf.record.RecordFactoryInputStream.readNextRecord(RecordFactoryInputStream.java:270)
	at org.apache.poi.hssf.record.RecordFactoryInputStream.nextRecord(RecordFactoryInputStream.java:236)
	at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:392)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:278)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:203)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:319)
	at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:300)
	at poi.TestPOI.testGetDefaultColumnWidthForHSSF(TestPOI.java:437)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.apache.poi.hssf.record.RecordFormatException: Not enough data (0) to read requested (2) bytes
	at org.apache.poi.hssf.record.RecordInputStream.checkRecordPosition(RecordInputStream.java:215)
	at org.apache.poi.hssf.record.RecordInputStream.readUShort(RecordInputStream.java:260)
	at org.apache.poi.hssf.record.DefaultColWidthRecord.<init>(DefaultColWidthRecord.java:41)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
	at org.apache.poi.hssf.record.RecordFactory$ReflectionRecordCreator.create(RecordFactory.java:56)
	... 33 more

2.For XSSF
  //In  XSSFSheet.getDefaultColumnWidth
    public int getDefaultColumnWidth() {
        CTSheetFormatPr pr = worksheet.getSheetFormatPr();
        return pr == null ? 8 : (int)pr.getBaseColWidth();
        //1. When pt is null ,return 8.It is error .Because I read excel 2007   
        //help default width is 8.43 char width
        //2. When pt not null or default width is adjusted,pr.getBaseCol is   
       //error,It will return 8 forever.Should call pr.getDefaultColWidth(),But 
       //return type is int !
    }
Comment 1 Yegor Kozlov 2011-06-20 17:09:16 UTC
The bug with HSSFSheet.getDefaultColumnWidth() is not reproducible with the latest trunk.

Yegor