Apache POI
2017-01-09 16:23
78 查看
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft
Office格式档案读和写的功能。
结构:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
Java代码
package com.sinosoft.Test;
import java.io.File;
import java.io.FileNotFoundException;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.HSSFColor;
public class TestExcel {
public static void main(String[] args) {
HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
sheet.createFreezePane(0, 1);// 冻结
// 设置列宽
sheet.setColumnWidth(0, 1000);
sheet.setColumnWidth(1, 3500);
sheet.setColumnWidth(2, 3500);
sheet.setColumnWidth(3, 6500);
sheet.setColumnWidth(4, 6500);
sheet.setColumnWidth(5, 6500);
sheet.setColumnWidth(6, 6500);
sheet.setColumnWidth(7, 2500);
sheet.createRow(0);
sheet.setDefaultRowHeight((short) 2500);
// 设置字体
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("隶书");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);// 上边框的颜色
columnHeadStyle.setBorderTop((short) 2);// 边框的大小
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 2);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 2);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); // 设置单元格的边框为双向
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
//创建行
HSSFRow row2 = sheet.createRow(1);
//写中间部分的字
HSSFCell cell = row2.createCell(0);
cell.setCellValue(new HSSFRichTextString("责任者"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(1);
cell.setCellValue(new HSSFRichTextString("成熟度排序"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(2);
cell.setCellValue(new HSSFRichTextString("事项"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(3);
cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(4);
cell.setCellValue(new HSSFRichTextString("上周工作进展"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(5);
cell.setCellValue(new HSSFRichTextString("本周工作计划"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(6);
cell.setCellValue(new HSSFRichTextString("问题和建议"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(7);
cell.setCellValue(new HSSFRichTextString("备 注"));
cell.setCellStyle(columnHeadStyle);
try {
FileOutputStream outputStream = new FileOutputStream("e://abc.xls");
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Office格式档案读和写的功能。
结构:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
Java代码
package com.sinosoft.Test;
import java.io.File;
import java.io.FileNotFoundException;
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.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
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.hssf.util.HSSFColor;
public class TestExcel {
public static void main(String[] args) {
HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件
HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet
sheet.createFreezePane(0, 1);// 冻结
// 设置列宽
sheet.setColumnWidth(0, 1000);
sheet.setColumnWidth(1, 3500);
sheet.setColumnWidth(2, 3500);
sheet.setColumnWidth(3, 6500);
sheet.setColumnWidth(4, 6500);
sheet.setColumnWidth(5, 6500);
sheet.setColumnWidth(6, 6500);
sheet.setColumnWidth(7, 2500);
sheet.createRow(0);
sheet.setDefaultRowHeight((short) 2500);
// 设置字体
HSSFFont columnHeadFont = workbook.createFont();
columnHeadFont.setFontName("隶书");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 列头的样式
HSSFCellStyle columnHeadStyle = workbook.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);// 上边框的颜色
columnHeadStyle.setBorderTop((short) 2);// 边框的大小
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 2);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 2);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); // 设置单元格的边框为双向
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
//创建行
HSSFRow row2 = sheet.createRow(1);
//写中间部分的字
HSSFCell cell = row2.createCell(0);
cell.setCellValue(new HSSFRichTextString("责任者"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(1);
cell.setCellValue(new HSSFRichTextString("成熟度排序"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(2);
cell.setCellValue(new HSSFRichTextString("事项"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(3);
cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(4);
cell.setCellValue(new HSSFRichTextString("上周工作进展"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(5);
cell.setCellValue(new HSSFRichTextString("本周工作计划"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(6);
cell.setCellValue(new HSSFRichTextString("问题和建议"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(7);
cell.setCellValue(new HSSFRichTextString("备 注"));
cell.setCellStyle(columnHeadStyle);
try {
FileOutputStream outputStream = new FileOutputStream("e://abc.xls");
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 利用apache poi读取excel的一个小例子
- Apache POI 应用
- Apache POI API操作Microsoft Office格式文件的方法+JAVA调用exe可执行文件
- Apache POI 简介
- Apache POI
- 使用Apache POI抽取OFFICE文本(DOC, DOCX, XLS, XLSX, PPT, PPTX)——Desktop Search开发笔记【经验积累】
- jxl(java Excel Api)-apache POI(HSSF - XSSF - SXSSF) selenium数据源--Excel
- java使用Apache POI操作excel文件
- apache POI 的 HSSF和XSSF
- Apache POI 简介
- (2) 如何用Apache POI操作Excel文件-----如何在已有的Excel文件中插入一行新的数据?
- 使用Apache POI API读写Excel
- SpringMVC + Apache POI 实现WEB中Excel下载功能
- Apache POI
- Apache POI组件操作Excel,制作报表(一)
- Apache POI 3.14 发布
- Java 操作 excel 文档 用 apache poi 来解决。
- Apache POI介绍
- Apache POI使用详解
- <学习笔记>Android Studio使用Apache POI在Android中生成.xls的Excel文件