java 如何在apache POI中自动调整excel中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40130927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to auto adjust the column in excel in apache POI
提问by storyteller
I am creating an excel file with apache poi the excel is generated but i can not adjust the column with according to the cell values i am posting the code what i have done so far
我正在使用 apache poi 创建一个 excel 文件,生成了 excel,但我无法根据单元格值调整列 我正在发布代码我到目前为止所做的
This is how i have created the headers in excel
这就是我在 excel 中创建标题的方式
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
sheet.protectSheet("password");
sheet.autoSizeColumn(15);
HSSFFont hSSFFont = wb.createFont();
hSSFFont.setFontName(HSSFFont.FONT_ARIAL);
hSSFFont.setFontHeightInPoints((short) 8);
CellStyle style = wb.createCellStyle();
/* cell style for locking */
CellStyle lockedCellStyle = wb.createCellStyle();
lockedCellStyle.setLocked(true);
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.createRow(0);
int headercolumnNo = 0;
//1st Column Header for Indicator
cell = row.createCell(headercolumnNo);
cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(0)));
style.setWrapText(true);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFont(hSSFFont);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(style);
headercolumnNo = 1;
cell = row.createCell(headercolumnNo); //2nd Column Header for Firstname
cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(1)));
style.setWrapText(true);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFont(hSSFFont);
cell.setCellStyle(style);
headercolumnNo = headercolumnNo + 1;
cell = row.createCell(headercolumnNo); //2nd Column Header for Firstname
cell.setCellValue(new HSSFRichTextString(listOfActiveCarrierUserHeader.get(2)));
style.setWrapText(true);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFont(hSSFFont);
cell.setCellStyle(style);
headercolumnNo = headercolumnNo + 1;
and this is how i have populated the values in that excel file
for(CarrierActiveUser carrierActiveUser : listOfCarrierUser){
int columnNo = 0;
row = sheet.createRow(j + 1);
cell = row.createCell(columnNo);
if(null != carrierActiveUser.getFistName()){
cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getFistName()));
lockedCellStyle.setFont(hSSFFont);
cell.setCellStyle(lockedCellStyle);
}else{
cell.setCellValue(new HSSFRichTextString(" "));
cell.setCellStyle(lockedCellStyle);
}
columnNo = columnNo + 1;
cell = row.createCell(columnNo);
if(null != carrierActiveUser.getLastName()){
cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getLastName()));
lockedCellStyle.setFont(hSSFFont);
cell.setCellStyle(lockedCellStyle);
}else{
cell.setCellValue(new HSSFRichTextString(" "));
cell.setCellStyle(lockedCellStyle);
}
columnNo = columnNo + 1;
cell = row.createCell(columnNo);
if(null != carrierActiveUser.getLastName()){
cell.setCellValue(new HSSFRichTextString(carrierActiveUser.getEmailId()));
lockedCellStyle.setFont(hSSFFont);
cell.setCellStyle(lockedCellStyle);
}else{
cell.setCellValue(new HSSFRichTextString(" "));
cell.setCellStyle(lockedCellStyle);
}
Please someone help me to adjust the columns , i am new to apache poi
请有人帮我调整列,我是 apache poi 的新手
回答by Ravikumar
You can use HSSFSheet.autoSizeColumn(columnNumber)
method to align the columns perfectly.
您可以使用HSSFSheet.autoSizeColumn(columnNumber)
方法来完美对齐列。
This method adjusts the column width to fit the contents, read the doc.
此方法调整列宽以适应内容,请阅读doc。
After setting all cell values for all columns you can use this method, in your current code call this method after for loop.
为所有列设置所有单元格值后,您可以使用此方法,在当前代码中,在 for 循环后调用此方法。
Sample code
示例代码
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);
Note - You have to do this separately for all columns which you want to be aligned and the call to sheet.autoSizeColumn(columnNumber)
should be made after populating the data into the excel. Calling before populating data will not have any effect.
注意 - 您必须对要对齐的所有列分别执行此操作,并且sheet.autoSizeColumn(columnNumber)
应在将数据填充到 excel 后进行调用。在填充数据之前调用不会有任何影响。