gpt4 book ai didi

java - 如何在apache POI中自动调整excel中的列

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:22:32 27 4
gpt4 key购买 nike

我正在使用 apache poi 创建一个 excel 文件,生成了 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);
}

请有人帮我调整列,我是 apache poi 的新手

Current output

最佳答案

您可以使用 HSSFSheet.autoSizeColumn(columnNumber) 方法来完美对齐列。

此方法调整列宽以适应内容,阅读 doc .

在为所有列设置所有单元格值后,您可以使用此方法,在您当前的代码中,在 for 循环之后调用此方法。

示例代码

sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);

注意 - 您必须对所有要对齐的列单独执行此操作,并且应在将数据填充到 excel 后调用 sheet.autoSizeColumn(columnNumber)。在填充数据之前调用不会有任何影响。

关于java - 如何在apache POI中自动调整excel中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40130927/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com