Generate complex tables (merge cells) by poi in Java

Effect picture:

Code:

package com.wangyun.util;


import java.io.FileOutputStream;
import java.io.IOException;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.util.Region;


public class ExcelTest {
/**
* @param args
*/
@SuppressWarnings("deprecation") 
public static void main(String[] args) throws IOException {


try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // style object


style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// vertical
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// horizontal
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);


sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
/ / ce.setEncoding(HSSFCell.ENCODING_UTF_16);// Chinese processing
ce.setCellValue("Ranking"); // Data displayed in the first row and first column of the table
ce.setCellStyle(style); // Style, centered
sheet. addMergedRegion(new Region(0, (short) 1, 1, (short) 1));
HSSFCell ce1 = row.createCell((short) 1);
// ce.setEncoding(HSSFCell.ENCODING_UTF_16);// Chinese processing
ce1 .setCellValue("City"); // The data displayed in the first row and first column of the table
ce1.setCellStyle(style); // style, center
sheet.addMergedRegion(new Region(0, (short) 2, 1, (short) 2));
HSSFCell ce2 = row.createCell((short) 2);
// ce.setEncoding(HSSFCell.ENCODING_UTF_16);// Chinese processing
ce2.setCellValue("Ranking change"); // Data displayed in the first row and first column of the table
ce2.setCellStyle(style); // Style, centered
sheet. addMergedRegion(new Region(0, (short) 3, 1, (short) 3));
HSSFCell ce3 = row.createCell((short) 3);
// ce.setEncoding(HSSFCell.ENCODING_UTF_16);// Chinese processing
ce3 .setCellValue("total score"); // data displayed in the first row and first column of the table
ce3.setCellStyle(style); // style, centered
int num = 0;
for (int i = 0; i < 7; i++) { // loop 9 times, each time display across cells
// calculate from that cell to that cell
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 3 ;
celle = 4;
} else {
celln = (i * 2+3);
celle = (i * 2 +4);
}
// cell merge
// The four parameters are: start row, start column, end row, end column
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1) );
cell.setCellValue("merging" + i); // data displayed across cells
cell.setCellStyle(style); // style
// data not displayed across cells, such as: divided into two lines, the previous line separately Two cells are one cell, and the next row is two cells, "quantity", "amount"
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
// cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("quantity");
cell1.setCellStyle(style);
// cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("Amount");
cell2.setCellStyle(style);
num++;
}


// Add the total percentage at the back


// The total is added at the end, and it also spans a cell
sheet.addMergedRegion(new Region(0, (short) (2 * num + 4), 0,(short) (2 * num + 5)));
HSSFCell cell = row.createCell((short) (2 * num + 4));
// cell.setEncoding (HSSFCell.ENCODING_UTF_16);
cell.setCellValue("Total");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 4));
HSSFCell cell2 = row2.createCell((short ) (2 * num + 5));
// cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("quantity");
cell1.setCellStyle(style);
// cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2. setCellValue("Amount");
cell2.setCellStyle(style);


// 百分比 同上
sheet.addMergedRegion(new Region(0, (short) (2 * num + 6), 0,
(short) (2 * num + 7)));
HSSFCell cellb = row.createCell((short) (2 * num + 6));
// cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百分比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 6));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 7));
// cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
// cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);


FileOutputStream fileOut = new FileOutputStream("E:\\workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}


}


}

Test yourself a few times, you will get the effect, I personally test

Reference address: Reference address


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325737121&siteId=291194637