版权声明:-万里晴空-祝你前途晴朗 https://blog.csdn.net/qq_35207917/article/details/86896253
入门程序
创建一个 工作簿
一个xls就是一个工作簿
Workbook wb = new HSSFWorkbook();
...
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
Workbook wb = new XSSFWorkbook();
...
try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
wb.write(fileOut);
}
创建一个 薄片
一个工作簿含有多个 薄片
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
Sheet sheet1 = wb.createSheet("new sheet");
Sheet sheet2 = wb.createSheet("second sheet");
// Note that sheet name is Excel must not exceed 31 characters
// and must not contain any of the any of the following characters:
// 0x0000
// 0x0003
// colon (:)
// backslash (\)
// asterisk (*)
// question mark (?)
// forward slash (/)
// opening square bracket ([)
// closing square bracket (])
// You can use org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)}
// for a safe way to create valid names, this utility replaces invalid characters with a space (' ')
String safeName = WorkbookUtil.createSafeSheetName("[O'Brien's sales*?]"); // returns " O'Brien's sales "
Sheet sheet3 = wb.createSheet(safeName);
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
创建一个单元格
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
创建一个时间类型的单元格
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a date value in it. The first cell is not styled
// as a date.
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//you can also set date as java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
// Write the output to a file
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) {
wb.write(fileOut);
}
对齐
public void test4() throws IOException {
Workbook wb = new HSSFWorkbook(); //or new HSSFWorkbook();
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直居中
Sheet sheet = wb.createSheet();
Row row1 = sheet.createRow(0); //单元行从0开始
row1.setHeightInPoints(30); //行高度
Cell cell1 = row1.createCell(3); //在第四个位置上创建一个单元格
cell1.setCellValue("111sfsdf--sdfasdf"); //为单元格设置值
cell1.setCellStyle(cellStyle); //为单元添加样式
Row row = sheet.createRow(1);
row.setHeightInPoints(30); //高度
Cell cell = row.createCell(3);
cell.setCellValue("Align It11111111111111");
cell.setCellStyle(cellStyle);
OutputStream fileOut = new FileOutputStream("E:\\workbook.xls");
wb.write(fileOut);
}
图片
边框样式
public void test5() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1); //创建行
// Create a cell and put a value in it.
Cell cell = row.createCell(1); //创建单元格
cell.setCellValue(4);
// Style the cell with borders all around.
CellStyle style = wb.createCellStyle(); //创建一个样式
style.setBorderBottom(BorderStyle.THIN); //底部为细边
style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //为底部添加颜色 黑色
style.setBorderLeft(BorderStyle.THIN); //z左边
style.setLeftBorderColor(IndexedColors.GREEN.getIndex()); //绿色
style.setBorderRight(BorderStyle.THIN); //右边
style.setRightBorderColor(IndexedColors.BLUE.getIndex()); //蓝色
style.setBorderTop(BorderStyle.MEDIUM_DASHED); //顶部 虚线
style.setTopBorderColor(IndexedColors.BLACK.getIndex()); //黑色
// style.setFillBackgroundColor(IndexedColors.RED.getIndex()); //填充背景色
cell.setCellStyle(style);
OutputStream fileOut = new FileOutputStream("E:\\workbookB.xls");
wb.write(fileOut);
}
图片
填充颜色
public void test6() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
// Aqua background
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); //要填充的颜色为浅绿色
style.setFillPattern(FillPatternType.BIG_SPOTS); //背景颜色的样式视点状
Cell cell = row.createCell(1); //创建单元格
cell.setCellValue("X"); //存入字符
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); //要填充的背景色为橙色
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //为固体前景色,也就是去不填充和
cell = row.createCell(2); //创建单元格
cell.setCellValue("X"); //填充字符串
cell.setCellStyle(style); //设置样式
OutputStream fileOut = new FileOutputStream("E:\\workbookB.xls"); //创建输出流
wb.write(fileOut); //write 出去,将这个工作波
fileOut.close();
wb.close();
}
截图
合并单元格
public void test7() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow(1);
Cell cell = row.createCell(1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new CellRangeAddress( //指定那些行那些列需要合并
1, //first row (0-based)
1, //last row (0-based)
1, //first column (0-based)
6 //last column (0-based)
));
Cell cell1 = row.createCell(7);
cell1.setCellValue("11111111");
OutputStream fileOut = new FileOutputStream("E:\\workbookB.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
截图
字体
public void test8() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(1);
// Create a new font and alter it.
Font font = wb.createFont(); //为单元格创建一个字体
font.setFontHeightInPoints((short)24); //设置字体的大小
font.setFontName("Courier New"); //设置字体的名字 宋体,楷体
font.setItalic(true); //使用斜体
font.setStrikeout(true); //添加删除线
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle(); //字体需要添加到单元格的样式中,从能使用
style.setFont(font); //添加设置的字体
// Create a cell and put a value in it.
Cell cell = row.createCell(1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
// Write the output to a file
OutputStream fileOut = new FileOutputStream("E:\\workbookFont.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
demo测试
public void test9() throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row title = sheet.createRow(0); //标题行
title.setHeightInPoints(36); //高度
//创建标题
Cell titleCell = title.createCell(0); //创建一个单元格
sheet.addMergedRegion(new CellRangeAddress( //指定这个标题合并的单元格和行数
0, //first row (0-based)
0, //last row (0-based)
0, //first column (0-based)
5 //last column (0-based)
));
//设置字体
Font font = wb.createFont();
font.setFontHeightInPoints((short)28);
font.setFontName("Courier New");
font.setItalic(true);
// Fonts are set into a style so create a new one to use.
CellStyle style = wb.createCellStyle();
style.setFont(font);
//设置背景色
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置对齐样式
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
titleCell.setCellValue("班级汇报情况");
titleCell.setCellStyle(style);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置每一列的标题
Row row1 = sheet.createRow(1);
row1.setHeightInPoints(25);
for (int i=0;i<6;i++){
Cell cell = row1.createCell(i);
cell.setCellStyle(cellStyle);
cell.setCellValue("姓名");
}
//设置内容
for (int i=2;i<100;i++){
Row row = sheet.createRow(i);
row.setHeightInPoints(25);
for (int j=0;j<6;j++){
sheet.autoSizeColumn(j);//宽度自适应
Cell cell = row.createCell(j);
cell.setCellValue("姓名王大海45858555");
cell.setCellStyle(cellStyle);
}
}
OutputStream fileOut = new FileOutputStream("E:\\workbookText.xls");
wb.write(fileOut);
fileOut.close();
wb.close();
}
截图