1. 程式人生 > >ApachePOI建立複雜表格

ApachePOI建立複雜表格

ApachePOI教程:https://www.yiibai.com/apache_poi/
1.先匯入ApachePOI需要的jar包
第一張
第二張
第三張
2.簡單測試類
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.;
public class ApaPOIExcelTest {
public static void main(String[] args) throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook();//建立一個工作簿
XSSFSheet sheet = workbook.createSheet(“xxx”);//建立一個表格
Font font=workbook.createFont();//設定字型
font.setFontName(“宋體”);
((XSSFFont) font).setBold(true);
XSSFCellStyle style = workbook.createCellStyle();//建立樣式
style.setAlignment(XSSFCellStyle.ALIGN_CENTER);//設定樣式水平居中
style.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//設定垂直居中
style.setFont(font);
XSSFRow row1=sheet.createRow((short)0);//建立第一行
row1.setHeight((short)500);//設定行高
XSSFCell cell1 = (XSSFCell) row1.createCell((short) 0);//建立第一個單元格
cell1.setCellValue(“小星星”);//設定單元格值
sheet.addMergedRegion(new CellRangeAddress(
0, //first row (0-based)
0, //last row (0-based)
0, //first column (0-based)
8 //last column (0-based)
));//合併單元格
cell1.setCellStyle(style);
XSSFRow row2=sheet.createRow((short)1);//建立第二行
XSSFCell cell2_One = (XSSFCell) row2.createCell((short)0);//第2行1單元格
cell2_One.setCellValue(“鄉鎮(街道)”);//單元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
2, //last row (0-based)
0, //first column (0-based)
0 //last column (0-based)
));//合併單元格
sheet.setColumnWidth(0,256

15+184);//設定列寬公式256*寬+184,得到值15.09
cell2_One.setCellStyle(style);
XSSFCell cell2_two = (XSSFCell) row2.createCell((short)1);//第二單元格
cell2_two.setCellValue(“總數”);//單元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
2, //last row (0-based)
1, //first column (0-based)
1 //last column (0-based)
));//合併單元格
cell2_two.setCellStyle(style);
XSSFCell cell2_three = (XSSFCell) row2.createCell((short) 2);//第3單元格
cell2_three.setCellValue(“已採集”);//單元格值
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
2, //first column (0-based)
4 //last column (0-based)
));//合併單元格
cell2_three.setCellStyle(style);
XSSFCell cell2_five = (XSSFCell) row2.createCell((short) 5);//第6單元格
cell2_five.setCellValue(“未採集”);
sheet.addMergedRegion(new CellRangeAddress(
1, //first row (0-based)
1, //last row (0-based)
5, //first column (0-based)
7 //last column (0-based)
));//合併單元格
cell2_five.setCellStyle(style);
XSSFRow row3=sheet.createRow((short)2);//建立第三行
XSSFCell cell3_three = (XSSFCell) row3.createCell((short)2);//第3單元格
cell3_three.setCellValue(“企業職工”);//單元格值
cell3_three.setCellStyle(style);
XSSFCell cell3_four = (XSSFCell) row3.createCell((short)3);//第4單元格
cell3_four.setCellValue(“機關事業”);//單元格值
cell3_four.setCellStyle(style);
XSSFCell cell3_five = (XSSFCell) row3.createCell((short)4);//第5單元格
cell3_five.setCellValue(“城鎮居民”);//單元格值
cell3_five.setCellStyle(style);
XSSFCell cell3_six = (XSSFCell) row3.createCell((short)5);//第六單元格
cell3_six.setCellValue(“企業職工”);//單元格值
cell3_six.setCellStyle(style);
XSSFCell cell3_seven = (XSSFCell) row3.createCell((short)6);//第七單元格
cell3_seven.setCellValue(“機關事業”);//單元格值
cell3_seven.setCellStyle(style);
XSSFCell cell3_eight = (XSSFCell) row3.createCell((short)7);//第八單元格
cell3_eight.setCellValue(“城鎮居民”);//單元格值
cell3_eight.setCellStyle(style);
FileOutputStream out = new FileOutputStream(
new File(“D:\Idea\photos\xxx.xlsx”));
workbook.write(out);
out.close();
}
}
3.效果如圖
在這裡插入圖片描述