0
点赞
收藏
分享

微信扫一扫

使用POI让java可以操作Excel(2)

Star英 2022-09-30 阅读 100


1. 基本使用

  • 设置文本对齐方式

mport java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo8 {

public static void main(String[] args) throws Exception {
Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(2); // 创建一个行
row.setHeightInPoints(30); // 设定行高

createCell(wb, row, 0, HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
createCell(wb, row, 1, HorizontalAlignment.FILL, VerticalAlignment.CENTER);
createCell(wb, row, 2, HorizontalAlignment.LEFT, VerticalAlignment.TOP);
createCell(wb, row, 3, HorizontalAlignment.RIGHT, VerticalAlignment.TOP);

FileOutputStream fileOut = new FileOutputStream("e:/tmp/致青春.xls");
wb.write(fileOut);
fileOut.close();
}

/**
* 创建一个单元格并为其设定指定的对其方式
*
* @param wb 工作簿
* @param row 行
* @param column 列
* @param halign 水平方向对其方式
* @param valign 垂直方向对其方式
*/
private static void createCell(Workbook wb, Row row, int column, HorizontalAlignment halign,
VerticalAlignment valign) {
Cell cell = row.createCell(column); // 创建单元格
cell.setCellValue(new HSSFRichTextString("Align It")); // 设置值
CellStyle cellStyle = wb.createCellStyle(); // 创建单元格样式
cellStyle.setAlignment(halign); // 设置单元格水平方向对其方式
cellStyle.setVerticalAlignment(valign); // 设置单元格垂直方向对其方式
cell.setCellStyle(cellStyle); // 设置单元格样式
}
}

  • 边框处理

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo9 {

public static void main(String[] args) throws Exception {
Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(1); // 创建一个行

Cell cell = row.createCell(1); // 创建一个单元格
cell.setCellValue(4);

CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN); // 底部边框
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); // 底部边框颜色

cellStyle.setBorderLeft(BorderStyle.THIN); // 左边边框
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex()); // 左边边框颜色

cellStyle.setBorderRight(BorderStyle.THIN); // 右边边框
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); // 右边边框颜色

cellStyle.setBorderTop(BorderStyle.MEDIUM_DASHED); // 上边边框
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); // 上边边框颜色

cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("e:/tmp/你好.xls");
wb.write(fileOut);
fileOut.close();
}
}

  • 设置单元格的前景/背景色

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class Demo10 {

public static void main(String[] args) throws Exception {
Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(1); // 创建一个行

Cell cell = row.createCell(1);
cell.setCellValue("XX");
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); // 背景色
cellStyle.setFillPattern(FillPatternType.BIG_SPOTS);
cell.setCellStyle(cellStyle);

Cell cell2 = row.createCell(2);
cell2.setCellValue("YYY");
CellStyle cellStyle2 = wb.createCellStyle();
cellStyle2.setFillForegroundColor(IndexedColors.RED.getIndex()); // 前景色
cellStyle2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell2.setCellStyle(cellStyle2);

FileOutputStream fileOut = new FileOutputStream("e:/tmp/balaba.xls");
wb.write(fileOut);
fileOut.close();
}
}

  • 单元格合并

import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;

public class Demo11 {

public static void main(String[] args) throws Exception {
Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(1); // 创建一个行

Cell cell = row.createCell(1);
cell.setCellValue("单元格合并测试");

sheet.addMergedRegion(new CellRangeAddress(1, // 起始行
2, // 结束行
1, // 起始列
2 // 结束列
));

FileOutputStream fileOut = new FileOutputStream("e:/tmp/test1.xls");
wb.write(fileOut);
fileOut.close();
}
}

  • 字体处理

Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(1); // 创建一个行

// 创建一个字体处理类
Font font = wb.createFont();
font.setFontHeightInPoints((short) 24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);

CellStyle style = wb.createCellStyle();
style.setFont(font);

Cell cell = row.createCell((short) 1);
cell.setCellValue("This is test of fonts");
cell.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("E:\\tmp\\工作簿.xls");
wb.write(fileOut);
fileOut.close();

2. 高级使用

  • 读取和重写工作簿
    可以根据模版来生成/导出Excel表格。

InputStream inp = new FileInputStream("e:/tmp/工作簿.xls");
POIFSFileSystem fs = new POIFSFileSystem(inp);
Workbook wb = new HSSFWorkbook(fs);
Sheet sheet = wb.getSheetAt(0); // 获取第一个Sheet页
Row row = sheet.getRow(0); // 获取第一行。如果第一行没有任何数据,那么row就为null。
Cell cell = row.getCell(0); // 获取单元格
if (cell == null) {
cell = row.createCell(3);
}
cell.setCellType(CellType.STRING);
cell.setCellValue("测试单元格");

FileOutputStream fileOut = new FileOutputStream("e:/tmp/工作簿.xls");
wb.write(fileOut);
fileOut.close();

  • 在单元格中换行

Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
Row row = sheet.createRow(2); // 创建一个行
Cell cell = row.createCell(2);
cell.setCellValue("我要换行 \n 成功了吗?");

CellStyle cs = wb.createCellStyle();
// 设置可以换行
cs.setWrapText(true);
cell.setCellStyle(cs);

// 调整下行的高度
row.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
// 调整单元格宽度
sheet.autoSizeColumn(2);

FileOutputStream fileOut = new FileOutputStream("e:/tmp/测试.xls");
wb.write(fileOut);
fileOut.close();

  • 创建用户自定义数据格式

Workbook wb = new HSSFWorkbook(); // 定义一个新的工作簿
Sheet sheet = wb.createSheet("第一个Sheet页"); // 创建第一个Sheet页
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(111111.25);

style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0")); // 设置数据格式
cell.setCellStyle(style);

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(1111111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.000"));
cell.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("e:/tmp/测试1.xls");
wb.write(fileOut);
fileOut.close();


举报

相关推荐

0 条评论