apache POI 的 HSSF和XSSF
2014-12-24 16:36
441 查看
转自:http://blog.sina.com.cn/s/blog_717c2b0f0100wcef.html
没工夫看先放着
还有几个不错的:
第一个:点击打开链接
POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft
Office 办公套件如 Excel,Word,Powerpoint 等文件的 API.
微软的Office 办公软件在企业的日常办公中占据着重要的地位,人们已经非常熟悉
Office 的使用。在我们开发的应用系统中,常常需要将数据导出到 Excel 文件中,或者
Word 文件中进行打印。比如移动的话费查询系统中就提供了将话费清单导入到 excel 表
格中的功能。这样在web 应用中,我们在浏览器中看到的数据可以被导出到 Excel 中了。
Excel 文件: xls 格式文件对应 POI API 为 HSSF 。 xlsx 格式为 office 2007 的文件格式,POI 中对应的API 为XSSF
Word 文件:doc 格式文件对应的 POI API 为 HWPF。 docx 格式为 XWPF
powerPoint 文件:ppt 格式对应的 POI API 为 HSLF。 pptx 格式为 XSLF
outlook :对应的 API 为 HSMF
Visio: 对应的 API 为 HDGF
Publisher : 对应的 API 为 HPBF
第二个:点击打开链接
Apache POI HSSF和XSSF读写EXCEL总结
HSSF是指2007年以前的,XSSF是指2007年版本以上的
这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴
还有好多没有没有写的,详细的请参考http://poi.apache.org/spreadsheet/quick-guide.html
Java代码
public class SummaryHSSF {
public static void main(String[] args) throws IOException {
//创建Workbook对象(这一个对象代表着对应的一个Excel文件)
//HSSFWorkbook表示以xls为后缀名的文件
Workbook wb = new HSSFWorkbook();
//获得CreationHelper对象,这个应该是一个帮助类
CreationHelper helper = wb.getCreationHelper();
//创建Sheet并给名字(表示Excel的一个Sheet)
Sheet sheet1 = wb.createSheet("HSSF_Sheet_1");
Sheet sheet2 = wb.createSheet("HSSF_Sheet_2");
//Row表示一行Cell表示一列
Row row = null;
Cell cell = null;
for(int i=0;i<60;i=i+2){
//获得这个sheet的第i行
row = sheet1.createRow(i);
//设置行长度自动
//row.setHeight((short)500);
row.setHeightInPoints(20);
//row.setZeroHeight(true);
for(int j=0;j<25;j++){
//设置每个sheet每一行的宽度,自动,根据需求自行确定
sheet1.autoSizeColumn(j+1, true);
//创建一个基本的样式
CellStyle cellStyle = SummaryHSSF.createStyleCell(wb);
//获得这一行的每j列
cell = row.createCell(j);
if(j==0){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值
cell.setCellValue(true);
}else if(j==1){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//设置这个样式的格式(Format)
cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "#,##0.0000");
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值
cell.setCellValue(new Double(2008.2008));
}else if(j==2){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
cell.setCellStyle(cellStyle);
cell.setCellValue(helper.createRichTextString("RichString"+i+j));
}else if(j==3){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "MM-yyyy-dd");
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());
}else if(j==24){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
//设置公式
cell.setCellFormula("SUM(E"+(i+1)+":X"+(i+1)+")");
}else{
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
cell.setCellValue(1);
}
}
}
//输出
OutputStream os = new FileOutputStream(new File("c://SummaryHSSF.xls"));
wb.write(os);
os.close();
}
public static CellStyle createStyleCell(Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
//设置一个单元格边框颜色
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
//设置一个单元格边框颜色
cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
return cellStyle;
}
public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){
//设置上下
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//设置左右
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return cellStyle;
}
public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){
//还可以用其它方法创建format
cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
return cellStyle;
}
public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
//cellStyle.setFillBackgroundColor(bg);
cellStyle.setFillForegroundColor(fg);
cellStyle.setFillPattern(fp);
return cellStyle;
}
public static Font createFonts(Workbook wb){
//创建Font对象
Font font = wb.createFont();
//设置字体
font.setFontName("黑体");
//着色
font.setColor(HSSFColor.BLUE.index);
//斜体
font.setItalic(true);
//字体大小
font.setFontHeight((short)300);
return font;
}
}
读取Excel文件
public class ReadExcel {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream(new File("c://SummaryHSSF.xls"));
//根据输入流创建Workbook对象
Workbook wb = WorkbookFactory.create(is);
//get到Sheet对象
Sheet sheet = wb.getSheetAt(0);
//这个必须用接口
for(Row row : sheet){
for(Cell cell : row){
//cell.getCellType是获得cell里面保存的值的type
//如Cell.CELL_TYPE_STRING
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
System.out.print(cell.getBooleanCellValue()+" ");
break;
case Cell.CELL_TYPE_NUMERIC:
//先看是否是日期格式
if(DateUtil.isCellDateFormatted(cell)){
//读取日期格式
System.out.print(cell.getDateCellValue()+" ");
}else{
//读取数字
System.out.print(cell.getNumericCellValue()+" ");
}
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
System.out.print(cell.getCellFormula()+" ");
break;
case Cell.CELL_TYPE_STRING:
//读取String
System.out.print(cell.getRichStringCellValue().toString()+" ");
break;
}
}
System.out.println("");
}
}
}
还有一种传统的读法
Sheet sheet = wb.getSheetAt(0);
for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) {
Row row = (Row)rit.next();
for (Iterator cit = row.cellIterator(); cit.hasNext(); ) {
Cell cell = (Cell)cit.next();
// Do something here
}
}
HSSFSheet sheet = wb.getSheetAt(0);
for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {
HSSFRow row = rit.next();
for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>)row.cellIterator(); cit.hasNext(); ) {
HSSFCell cell = cit.next();
// Do something here
}
}
没工夫看先放着
还有几个不错的:
第一个:点击打开链接
POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft
Office 办公套件如 Excel,Word,Powerpoint 等文件的 API.
微软的Office 办公软件在企业的日常办公中占据着重要的地位,人们已经非常熟悉
Office 的使用。在我们开发的应用系统中,常常需要将数据导出到 Excel 文件中,或者
Word 文件中进行打印。比如移动的话费查询系统中就提供了将话费清单导入到 excel 表
格中的功能。这样在web 应用中,我们在浏览器中看到的数据可以被导出到 Excel 中了。
Excel 文件: xls 格式文件对应 POI API 为 HSSF 。 xlsx 格式为 office 2007 的文件格式,POI 中对应的API 为XSSF
Word 文件:doc 格式文件对应的 POI API 为 HWPF。 docx 格式为 XWPF
powerPoint 文件:ppt 格式对应的 POI API 为 HSLF。 pptx 格式为 XSLF
outlook :对应的 API 为 HSMF
Visio: 对应的 API 为 HDGF
Publisher : 对应的 API 为 HPBF
第二个:点击打开链接
Apache POI HSSF和XSSF读写EXCEL总结
HSSF是指2007年以前的,XSSF是指2007年版本以上的
这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴
还有好多没有没有写的,详细的请参考http://poi.apache.org/spreadsheet/quick-guide.html
Java代码
public class SummaryHSSF {
public static void main(String[] args) throws IOException {
//创建Workbook对象(这一个对象代表着对应的一个Excel文件)
//HSSFWorkbook表示以xls为后缀名的文件
Workbook wb = new HSSFWorkbook();
//获得CreationHelper对象,这个应该是一个帮助类
CreationHelper helper = wb.getCreationHelper();
//创建Sheet并给名字(表示Excel的一个Sheet)
Sheet sheet1 = wb.createSheet("HSSF_Sheet_1");
Sheet sheet2 = wb.createSheet("HSSF_Sheet_2");
//Row表示一行Cell表示一列
Row row = null;
Cell cell = null;
for(int i=0;i<60;i=i+2){
//获得这个sheet的第i行
row = sheet1.createRow(i);
//设置行长度自动
//row.setHeight((short)500);
row.setHeightInPoints(20);
//row.setZeroHeight(true);
for(int j=0;j<25;j++){
//设置每个sheet每一行的宽度,自动,根据需求自行确定
sheet1.autoSizeColumn(j+1, true);
//创建一个基本的样式
CellStyle cellStyle = SummaryHSSF.createStyleCell(wb);
//获得这一行的每j列
cell = row.createCell(j);
if(j==0){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值
cell.setCellValue(true);
}else if(j==1){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//设置这个样式的格式(Format)
cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "#,##0.0000");
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值
cell.setCellValue(new Double(2008.2008));
}else if(j==2){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
cell.setCellStyle(cellStyle);
cell.setCellValue(helper.createRichTextString("RichString"+i+j));
}else if(j==3){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, "MM-yyyy-dd");
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());
}else if(j==24){
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
//设置公式
cell.setCellFormula("SUM(E"+(i+1)+":X"+(i+1)+")");
}else{
cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
cell.setCellValue(1);
}
}
}
//输出
OutputStream os = new FileOutputStream(new File("c://SummaryHSSF.xls"));
wb.write(os);
os.close();
}
public static CellStyle createStyleCell(Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
//设置一个单元格边框颜色
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
//设置一个单元格边框颜色
cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
return cellStyle;
}
public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){
//设置上下
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//设置左右
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
return cellStyle;
}
public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){
//还可以用其它方法创建format
cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
return cellStyle;
}
public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
//cellStyle.setFillBackgroundColor(bg);
cellStyle.setFillForegroundColor(fg);
cellStyle.setFillPattern(fp);
return cellStyle;
}
public static Font createFonts(Workbook wb){
//创建Font对象
Font font = wb.createFont();
//设置字体
font.setFontName("黑体");
//着色
font.setColor(HSSFColor.BLUE.index);
//斜体
font.setItalic(true);
//字体大小
font.setFontHeight((short)300);
return font;
}
}
读取Excel文件
public class ReadExcel {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream(new File("c://SummaryHSSF.xls"));
//根据输入流创建Workbook对象
Workbook wb = WorkbookFactory.create(is);
//get到Sheet对象
Sheet sheet = wb.getSheetAt(0);
//这个必须用接口
for(Row row : sheet){
for(Cell cell : row){
//cell.getCellType是获得cell里面保存的值的type
//如Cell.CELL_TYPE_STRING
switch(cell.getCellType()){
case Cell.CELL_TYPE_BOOLEAN:
//得到Boolean对象的方法
System.out.print(cell.getBooleanCellValue()+" ");
break;
case Cell.CELL_TYPE_NUMERIC:
//先看是否是日期格式
if(DateUtil.isCellDateFormatted(cell)){
//读取日期格式
System.out.print(cell.getDateCellValue()+" ");
}else{
//读取数字
System.out.print(cell.getNumericCellValue()+" ");
}
break;
case Cell.CELL_TYPE_FORMULA:
//读取公式
System.out.print(cell.getCellFormula()+" ");
break;
case Cell.CELL_TYPE_STRING:
//读取String
System.out.print(cell.getRichStringCellValue().toString()+" ");
break;
}
}
System.out.println("");
}
}
}
还有一种传统的读法
Sheet sheet = wb.getSheetAt(0);
for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) {
Row row = (Row)rit.next();
for (Iterator cit = row.cellIterator(); cit.hasNext(); ) {
Cell cell = (Cell)cit.next();
// Do something here
}
}
HSSFSheet sheet = wb.getSheetAt(0);
for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {
HSSFRow row = rit.next();
for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>)row.cellIterator(); cit.hasNext(); ) {
HSSFCell cell = cit.next();
// Do something here
}
}
相关文章推荐
- Apache POI —— HSSF / XSSF(1)
- Apache POI HSSF,XSSF和SXSSF的区别
- Apache POI HSSF和XSSF读写EXCEL总结
- jxl(java Excel Api)-apache POI(HSSF - XSSF - SXSSF) selenium数据源--Excel
- Apache POI HSSF 解析Excel时的细节.
- HSSF,XSSF和SXSSF的区别与HSSF测试Demo
- excel导入 HSSFWorkbook和XSSFWorkbook
- POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本
- POI操作Excel详解,HSSF和XSSF两种方式
- POI操作Excel详解,HSSF和XSSF两种方式
- POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本
- 使用Apache POI中的XSSF导出Excel
- POI Workbook接口和HSSFWorkbook对象和XSSFWorkbook对象操作相应excel版本
- POI3.5_HSSF_和XSSF_Excel操作快速入门
- org.apache.poi.hssf.usermodel.HSSFWorkbook和org.apache.poi.xssf.usermodel.XSSFWorkbook的区别
- Apache POI HSSF读写Excel总结
- 创建HSSFWorkbook或XSSFWorkbook对象的方法
- 【springmvc+mybatis项目实战】杰信商贸-27.POI由HSSF升级为XSSF
- POI-HSSF and POI-XSSF - Java API To Access Microsoft Excel Format Files
- HSSF,XSSF和SXSSF的区别