ASF Bugzilla – Attachment 23693 Details for
Bug 47206
XSSFSheet autoSizeColumn not considering all rows
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
Illustrates the incorrect behaviour
Main.java (text/java), 3.13 KB, created by
ROb
on 2009-05-19 10:47:10 UTC
(
hide
)
Description:
Illustrates the incorrect behaviour
Filename:
MIME Type:
Creator:
ROb
Created:
2009-05-19 10:47:10 UTC
Size:
3.13 KB
patch
obsolete
> >package Poi; > >import java.io.FileInputStream; >import java.io.FileOutputStream; >import java.sql.Connection; >import java.sql.DriverManager; >import java.sql.Statement; >import org.apache.poi.hssf.usermodel.HSSFWorkbook; >import org.apache.poi.ss.usermodel.WorkbookFactory; >import org.apache.poi.xssf.usermodel.XSSFCell; >import org.apache.poi.xssf.usermodel.XSSFRow; >import org.apache.poi.xssf.usermodel.XSSFSheet; >import org.apache.poi.xssf.usermodel.XSSFWorkbook; > >/** > * > * @author Rob Stewart > */ >public class Main { > > static XSSFWorkbook populatedXLSXWorkbook, wb; > > public static void main(String[] args) { > String filename = System.getProperty("user.dir") + "\\2007_Test.xlsx"; > System.out.println("File name: " + filename); > // Notice the steps here: > // 1. Create the workbook > // 2. Populate with data using jdbc > // 3. Resize the columns > > createWorkbook(filename); > addData(filename); > resizeXLSXColumns(filename); > } > > public static void createWorkbook(String fileName){ > try{ > wb = new XSSFWorkbook(); > String[] columns = {"Column_1", "Column_2_Extended", "Column_3"}; > createSheet("Sheet_number_1", columns); > FileOutputStream fileOut = new FileOutputStream(fileName); > wb.write(fileOut); > fileOut.close(); > } > catch(Exception v){System.out.println(v);} > } > > > public static void addData(String fileName){ > String DATABASE_URL = "jdbc:odbc:Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" + fileName + "; readOnly= false; IMEX=1"; > try{ > Connection con = DriverManager.getConnection(DATABASE_URL); > Statement stmt = con.createStatement(); > stmt.executeUpdate("insert into [Sheet_number_1$](Column_1,Column_2_Extended,Column_3) values ('A Very large string in column 1 AAAAAAAAAAAAAAAAAAAAA', 'foo', 'bar')"); > stmt.close(); > con.close(); > } > catch (Exception b){ System.out.println(b);} > } > > > public static void createSheet(String sheetName, String[] columns){ > XSSFSheet sheet = wb.createSheet(sheetName); > XSSFRow row = sheet.createRow(0); > for(int i=0;i<columns.length;i++){ > XSSFCell cell = row.createCell(i); > cell.setCellValue(columns[i].toString()); > } > } > > > > > public static void resizeXLSXColumns(String FILENAME){ > // open the Excel Spreadsheet > try{ > populatedXLSXWorkbook = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream(FILENAME)); > } > catch(Exception b){System.out.println(b);} > > int sheetCount = populatedXLSXWorkbook.getNumberOfSheets(); > XSSFSheet sheet; > for(int i=0;i<sheetCount;i++){ > sheet = (XSSFSheet) populatedXLSXWorkbook.getSheetAt(i); > for (int j=0;j<10;j++){ > sheet.autoSizeColumn(j); > } > } > // Now save changes > try{ > FileOutputStream fileOut = new FileOutputStream(FILENAME); > populatedXLSXWorkbook.write(fileOut); > fileOut.close(); >} >catch(Exception b){System.out.println(b);} > } > >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 47206
:
23670
|
23672
|
23673
| 23693