gpt4 book ai didi

java - Apache POI 没有设计整行的样式

转载 作者:行者123 更新时间:2023-12-01 11:20:35 25 4
gpt4 key购买 nike

我正在使用 Apache POI 生成一些报告,但在设置行样式时遇到问题。当我打开生成的 Excel 文件时,行的样式从第 25 列之后开始,我已经尝试了所有方法,但无法找出原因..

代码如下:

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


public class PrintExcel {

static void print(LinkedHashMap<RowReport,RowReport> map, String fileName) {

try {

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("DiffReport");

int iterator_row = 0,iterator_cell = 0;

CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.RED.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

//Iterate through rows

for(RowReport row : map.keySet()) {

Row rowDestination = sheet.createRow((short)iterator_row);

//cellDestination.setCellStyle(style);

for(iterator_cell = 0; iterator_cell <= 25; iterator_cell++) {

Cell cellDestination = rowDestination.createCell(iterator_cell);

switch (iterator_cell) {

case 0:
cellDestination.setCellValue(row.getPlace());
break;
case 1:
cellDestination.setCellValue(row.getPoint());
break;
case 2:
cellDestination.setCellValue(row.getSide());
break;
case 3:
cellDestination.setCellValue(row.getSub_sector());
break;
case 4:
cellDestination.setCellValue(row.getPosition());
break;
case 5:
cellDestination.setCellValue(row.getBeam());
break;
case 6:
cellDestination.setCellValue(row.getSector());
break;
case 7:
cellDestination.setCellValue(row.getTpg300_name());
break;
case 8:
cellDestination.setCellValue(row.getSecond_name());
break;
case 9:
cellDestination.setCellValue(row.getProgram());
break;
case 10:
cellDestination.setCellValue(row.getCard_A_type());
break;
case 11:
cellDestination.setCellValue(row.getCard_B_type());
break;
case 12:
cellDestination.setCellValue(row.getUnit());
break;
case 13:
cellDestination.setCellValue(row.getUnderange());
break;
case 14:
cellDestination.setCellValue(row.getA1());
break;
case 15:
cellDestination.setCellValue(row.getA2());
break;
case 16:
cellDestination.setCellValue(row.getB1());
break;
case 17:
cellDestination.setCellValue(row.getB2());
break;
case 18:
cellDestination.setCellValue(row.getCardinal());
break;
case 19:
cellDestination.setCellValue(row.getSource());
break;
case 20:
cellDestination.setCellValue(row.getGauge());
break;
case 21:
cellDestination.setCellValue(row.getFilter());
break;
case 22:
cellDestination.setCellValue(row.getValid());
break;
case 23:
cellDestination.setCellValue(row.getStatus());
break;
case 24:
cellDestination.setCellValue(row.getUpthreshold());
break;
case 25:
cellDestination.setCellValue(row.getLowthreshold());
break;

default:
break;
}

}

rowDestination.setRowStyle(style);
iterator_row++;

}

FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
wb.close();
fileOut.close();

} catch (EncryptedDocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}

最佳答案

您需要调用

rowDestination.setRowStyle(style)

在 for 循环中,在索引 <= 25 处终止。

只有在循环结束后才能调用它

关于java - Apache POI 没有设计整行的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31276600/

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