记录使用POI操作Excel2003、2007格式文件方法。
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>${poi.version}version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>${poi.version}version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxml-schemasartifactId>
<version>${poi.version}version>
dependency>
public class ExcelUtil {
/**
* 写Xls格式Excel文件
*
* @param path
* @throws Exception
*/
public static void printXls(String path) throws Exception {
try {
// 创建输出流
FileOutputStream out = new FileOutputStream(path);
// 创建Workbook
HSSFWorkbook wb = new HSSFWorkbook();
// 创建Sheet
HSSFSheet sheet = wb.createSheet();
// start 内容写入
HSSFRow row = sheet.createRow(0); // 指定列号
HSSFCell cell = row.createCell(0); // 指定行号
HSSFRichTextString richText = new HSSFRichTextString("中文字体测试");
cell.setCellValue(richText);
// 指定行号
HSSFCell enCell = row.createCell(3);
enCell.setCellValue(new HSSFRichTextString("English font test"));
// 设置列的宽度
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
// end 内容写入
// 创建PrintSetup
HSSFPrintSetup printSetup = sheet.getPrintSetup();
// A4纸
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
wb.write(out);
out.close();
} catch (Exception e) {
throw e;
}
}
/**
* 写Xlsx格式Excel文件
*
* @param path
* @throws Exception
*/
public static void printXlsx(String path) throws Exception {
try {
// 创建输出流
FileOutputStream out = new FileOutputStream(path);
// 创建Workbook
XSSFWorkbook wb = new XSSFWorkbook();
// 创建Sheet
XSSFSheet sheet = wb.createSheet();
// start 内容写入
XSSFRow row = sheet.createRow(0); // 指定列号
XSSFCell cell = row.createCell(0); // 指定行号
XSSFRichTextString richText = new XSSFRichTextString("中文字体测试");
cell.setCellValue(richText);
// 指定行号
XSSFCell enCell = row.createCell(3);
enCell.setCellValue(new XSSFRichTextString("English font test"));
// 设置列的宽度
sheet.setColumnWidth(2, 4000);
sheet.setColumnWidth(3, 4000);
// end 内容写入
// 创建PrintSetup
XSSFPrintSetup printSetup = sheet.getPrintSetup();
// A4纸
printSetup.setPaperSize(PrintSetup.A4_PAPERSIZE);
wb.write(out);
out.close();
} catch (Exception e) {
throw e;
}
}
/***
* 打印Xls格式文件
*
* @param path
* @throws Exception
*/
public static void writeXls(String path) throws Exception {
HSSFCell cell = null;
HSSFCellStyle cellstyle = null;
// 创建一个输出流
OutputStream os = new FileOutputStream(path);
// 创建一个工作簿
HSSFWorkbook work = new HSSFWorkbook();
// 创建一个工作表
HSSFSheet sheet = work.createSheet("sheet1");
// 创建行(第一行从0开始)
HSSFRow row = sheet.createRow(0);
// 创建单元格(第一列从0开始)
row.createCell(0).setCellValue("第一个单元格");
row.createCell(1).setCellValue("第二个单元格");
row.createCell(2).setCellValue(new Date());
// start 列号为3内容
// 通过workbook创建一个新的单元格风格(cell style)是很重要的
cellstyle = work.createCellStyle();
cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell(3);
cell.setCellValue(new Date());
cell.setCellStyle(cellstyle);
cellstyle = work.createCellStyle();
// end 列号为3内容
// start 列号为4内容
cell = row.createCell(4);
HSSFFont cnFont = work.createFont(); // 创建字体
cnFont.setFontHeightInPoints((short) 10); // 设置字体大小
cnFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
cnFont.setFontName("仿宋_GB2312"); // 字体名称
cellstyle.setFont(cnFont);
cell.setCellValue("李宁一切皆有可能!");
cell.setCellStyle(cellstyle);
// end 列号为4内容
// 创建工作表
HSSFSheet sh2 = work.createSheet("sheet2");
HSSFRow row2 = sh2.createRow((short) 0);
cell = row2.createCell(0);
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_RIGHT);
cell.setCellStyle(cellstyle);
cell.setCellValue("工作表二,第一列数据");
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
cell = row2.createCell(1);
cell.setCellStyle(cellstyle);
cell.setCellValue("==");
// 合并单元格1至5
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));
// 创建一个工作表
HSSFSheet sh3 = work.createSheet("sheet3");
HSSFPrintSetup ps = sh3.getPrintSetup();
sh3.setAutobreaks(true);
sh3.setSelected(true);
ps.setFitHeight((short) 1);
ps.setFitWidth((short) 1);
HSSFRow row3 = sh3.createRow(1);
// 列号为1
cell = row3.createCell(1);
// 设置样式
cellstyle = work.createCellStyle();
cellstyle.setBorderBottom(CellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellstyle.setBorderLeft(CellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor(HSSFColor.GREEN.index);
cellstyle.setBorderRight(CellStyle.BORDER_THIN);
cellstyle.setRightBorderColor(HSSFColor.BLUE.index);
cellstyle.setBorderTop(CellStyle.BORDER_THIN);
cellstyle.setTopBorderColor(HSSFColor.BLACK.index);
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
// 加入样式
cell.setCellStyle(cellstyle);
// 列号为2
HSSFRow row4 = sh3.createRow(2);
cell = row4.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
// 要启用新行,需要使用wrap=true设置单元格样式。
cellstyle = work.createCellStyle();
cellstyle.setWrapText(true);
cell.setCellStyle(cellstyle);
// 设置高度
row4.setHeightInPoints((2 * sh3.getDefaultRowHeightInPoints()));
sh3.autoSizeColumn((short) 2);
// 循环写入数据
for (int i = 1; i < 11; i++) {
HSSFRow row5 = sh2.createRow(i);
cell = row5.createCell(0);
cellstyle = work.createCellStyle();
HSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
cell.setCellStyle(cellstyle);
cell.setCellValue("测试");
}
cellstyle = work.createCellStyle();
HSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
for (int i = 1; i < 21; i++) {
HSSFRow row5 = sheet.createRow(i);
cell = row5.createCell(0);
cell.setCellStyle(cellstyle);
cell.setCellValue("开始");
}
work.write(os);
os.close();
}
/***
* 打印Xls格式文件
*
* @param path
* @throws Exception
*/
public static void writeXlsx(String path) throws Exception {
XSSFCell cell = null;
XSSFCellStyle cellstyle = null;
// 创建一个输出流
OutputStream os = new FileOutputStream(path);
// 创建一个工作簿
XSSFWorkbook work = new XSSFWorkbook();
// 创建一个工作表
XSSFSheet sheet = work.createSheet("sheet1");
// 创建行(第一行从0开始)
XSSFRow row = sheet.createRow(0);
// 创建单元格(第一列从0开始)
row.createCell(0).setCellValue("第一个单元格");
row.createCell(1).setCellValue("第二个单元格");
row.createCell(2).setCellValue(new Date());
// start 列号为3内容
// 通过workbook创建一个新的单元格风格(cell style)是很重要的
cellstyle = work.createCellStyle();
cellstyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell(3);
cell.setCellValue(new Date());
cell.setCellStyle(cellstyle);
cellstyle = work.createCellStyle();
// end 列号为3内容
// start 列号为4内容
cell = row.createCell(4);
XSSFFont cnFont = work.createFont(); // 创建字体
cnFont.setFontHeightInPoints((short) 10); // 设置字体大小
cnFont.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体显示
cnFont.setFontName("仿宋_GB2312"); // 字体名称
cellstyle.setFont(cnFont);
cell.setCellValue("李宁一切皆有可能!");
cell.setCellStyle(cellstyle);
// end 列号为4内容
// 创建工作表
XSSFSheet sh2 = work.createSheet("sheet2");
XSSFRow row2 = sh2.createRow((short) 0);
cell = row2.createCell(0);
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_RIGHT);
cell.setCellStyle(cellstyle);
cell.setCellValue("工作表二,第一列数据");
cellstyle = work.createCellStyle();
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
cell = row2.createCell(1);
cell.setCellStyle(cellstyle);
cell.setCellValue("==");
// 合并单元格1至5
sh2.addMergedRegion(new CellRangeAddress(0, 0, 1, 5));
// 创建一个工作表
XSSFSheet sh3 = work.createSheet("sheet3");
XSSFPrintSetup ps = sh3.getPrintSetup();
sh3.setAutobreaks(true);
sh3.setSelected(true);
ps.setFitHeight((short) 1);
ps.setFitWidth((short) 1);
XSSFRow row3 = sh3.createRow(1);
// 列号为1
cell = row3.createCell(1);
// 设置样式
cellstyle = work.createCellStyle();
cellstyle.setBorderBottom(CellStyle.BORDER_THIN);
cellstyle.setBottomBorderColor(HSSFColor.BLACK.index);
cellstyle.setBorderLeft(CellStyle.BORDER_THIN);
cellstyle.setLeftBorderColor(HSSFColor.GREEN.index);
cellstyle.setBorderRight(CellStyle.BORDER_THIN);
cellstyle.setRightBorderColor(HSSFColor.BLUE.index);
cellstyle.setBorderTop(CellStyle.BORDER_THIN);
cellstyle.setTopBorderColor(HSSFColor.BLACK.index);
cellstyle.setAlignment(CellStyle.ALIGN_FILL);
// 加入样式
cell.setCellStyle(cellstyle);
// 列号为2
XSSFRow row4 = sh3.createRow(2);
cell = row4.createCell(2);
cell.setCellValue("Use \n with word wrap on to create a new line");
// 要启用新行,需要使用wrap=true设置单元格样式。
cellstyle = work.createCellStyle();
cellstyle.setWrapText(true);
cell.setCellStyle(cellstyle);
// 设置高度
row4.setHeightInPoints((2 * sh3.getDefaultRowHeightInPoints()));
sh3.autoSizeColumn((short) 2);
// 循环写入数据
for (int i = 1; i < 11; i++) {
XSSFRow row5 = sh2.createRow(i);
cell = row5.createCell(0);
cellstyle = work.createCellStyle();
XSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
cell.setCellStyle(cellstyle);
cell.setCellValue("测试");
}
cellstyle = work.createCellStyle();
XSSFFont font = work.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellstyle.setFont(font);
for (int i = 1; i < 21; i++) {
XSSFRow row5 = sheet.createRow(i);
cell = row5.createCell(0);
cell.setCellStyle(cellstyle);
cell.setCellValue("开始");
}
work.write(os);
os.close();
}
/**
* 读取Xls格式的Excel文档
*
* @param path
* @return List
*/
public static List readXls(String path) throws Exception {
// 创建输入流
InputStream inp = new FileInputStream(path);
HSSFWorkbook workBook = new HSSFWorkbook(inp);
// 获得Excel中工作表个数
List list = new ArrayList();
int sheetCount = workBook.getNumberOfSheets();
System.out.println(sheetCount);
for (int i = 0; i < sheetCount; i++) {
String shname = workBook.getSheetName(i);// 工作表的名称
HSSFSheet sheet = workBook.getSheetAt(i);// 获取工作表
int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
System.out.println(rows);
if (rows > 0) {
for (int j = 0; j < rows; j++) {
HSSFRow row = sheet.getRow(j); // 取得当前行
if (row != null) {
int cells = row.getLastCellNum(); // 获取当前行列数
for (int k = 0; k < cells; k++) {
/** 获取当前列(就是一个单元格) */
HSSFCell cell = row.getCell(k);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String content = cell.getStringCellValue();
if (StringUtils.isNotEmpty(content)) {
// System.out.println("内容:"+content);
list.add(content);
}
}
}
}
}
}
}
}
return list;
}
/**
* 读取Xlsx格式的Excel文档
*
* @param path
* @return List
*/
public static List readXlsx(String path) throws Exception {
// 创建输入流
InputStream inp = new FileInputStream(path);
XSSFWorkbook workBook = new XSSFWorkbook(inp);
// 获得Excel中工作表个数
List list = new ArrayList();
int sheetCount = workBook.getNumberOfSheets();
System.out.println(sheetCount);
for (int i = 0; i < sheetCount; i++) {
String shname = workBook.getSheetName(i);// 工作表的名称
XSSFSheet sheet = workBook.getSheetAt(i);// 获取工作表
int rows = sheet.getPhysicalNumberOfRows(); // 获得行数
System.out.println(rows);
if (rows > 0) {
for (int j = 0; j < rows; j++) {
XSSFRow row = sheet.getRow(j); // 取得当前行
if (row != null) {
int cells = row.getLastCellNum(); // 获取当前行列数
for (int k = 0; k < cells; k++) {
/** 获取当前列(就是一个单元格) */
XSSFCell cell = row.getCell(k);
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String content = cell.getStringCellValue();
if (StringUtils.isNotEmpty(content)) {
// System.out.println("内容:"+content);
list.add(content);
}
}
}
}
}
}
}
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
// 读取测试
try {
List list = ExcelUtil.readXlsx("/Users/xianglingchuan/Downloads/test(1).xlsx");
for (String string : list) {
System.out.println(string);
}
list = ExcelUtil.readXls("/Users/xianglingchuan/Downloads/readxls.xls");
for (String string : list) {
System.out.println(string);
}
} catch (Exception e) {
e.printStackTrace();
}
// 输出测试
try {
//ExcelUtil.printXls("/Users/xianglingchuan/Downloads/test_print.xls");
//ExcelUtil.printXlsx("/Users/xianglingchuan/Downloads/test_print.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
try {
//ExcelUtil.writeXls("/Users/xianglingchuan/Downloads/test_write.xls");
//ExcelUtil.printXlsx("/Users/xianglingchuan/Downloads/test_print.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
}