gpt4 book ai didi

java - 如何在 Apache POI 中设置固定列宽

转载 作者:搜寻专家 更新时间:2023-10-31 08:22:04 24 4
gpt4 key购买 nike

如何在 Apache POI 中设置固定列宽。我想让我的第一列固定宽度。

我试过 sheet.setColumnWidth(0, 1000); cellStyle.setWrapText(true);//设置不反射(reflect)的自动换行

    public XSSFWorkbook generateReport(List<Dto> result, boolean isRes, boolean isRes1) {
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;
String[] headers = null;
int rowNum = 0;
int colNum = 0;
CellStyle cellStyle = null;
CellStyle headerStyle = null;
XSSFFont font = null;
CellStyle datecellStyle = null;
/* set the weight of the font */



try {
workbook = new XSSFWorkbook();

headers = new String[] { ...values goes here...};
row = sheet.createRow(rowNum);
font = workbook.createFont();
font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);

headerStyle = workbook.createCellStyle();
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
headerStyle.setFillForegroundColor((short) 200);
headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFont(font);

cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);



datecellStyle = workbook.createCellStyle();
datecellStyle.setDataFormat(workbook.getCreationHelper().createDataFormat().getFormat("dd-MMM-yyyy"));
datecellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
datecellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
datecellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
datecellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);



/**
* Writing Headers
*/
for (String header : headers) {
cell = row.createCell(colNum);
cell.setCellValue(header);
cell.setCellStyle(headerStyle);
++colNum;
}

/**
* Writing Other Rows
*/
SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
for (Dto detail : result) {
++rowNum;
colNum = 0;
row = sheet.createRow(rowNum);
cell = row.createCell(colNum);
//sheet.setColumnWidth(0, 4000);
cell.
if(null != detail.getGid()){
cell.setCellValue(detail.getGid());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(cellStyle);

++colNum;
cell = row.createCell(colNum);
if(null != detail.getName()){
cell.setCellValue(detail.getName());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(cellStyle);

++colNum;
cell = row.createCell(colNum);
if(null != detail.getNGid()){
cell.setCellValue(detail.getNGid());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(cellStyle);

++colNum;
cell = row.createCell(colNum);
if(null != detail.getName()){
cell.setCellValue(detail.getName());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(cellStyle);




}

for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i);
}
sheet.createFreezePane(1, 1);

} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}

最佳答案

您可以使用 XSSFWorkbooksetColumnWidth 方法设置列宽。第一个参数是列号(从零开始),第二个参数是宽度。我们需要在这里设置宽度有点棘手。要将宽度设置为 25,我们需要将参数传递为 25 * 256

XSSFSheet sheet = workbook.createSheet("MySheet");
sheet.setColumnWidth(3, 25 * 256);

关于java - 如何在 Apache POI 中设置固定列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42087117/

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