Apache POI没有样式整行

时间:2015-07-07 18:31:51

标签: java hashmap apache-poi

我正在使用Apache POI生成一些报告,而我在设计Rows时遇到了问题。当我打开生成的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();
    }


}

}

1 个答案:

答案 0 :(得分:1)

您需要致电

[]
在你的for循环中

终止于索引&lt; = 25。

您只能在此循环结束后调用