`
yangxianjiangg
  • 浏览: 62064 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Apache POI HSSF读写Excel总结

阅读更多

Apache POI HSSF和XSSF读写EXCEL总结
HSSF是指2007年以前的,XSSF是指2007年版本以上的
这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴
还有好多没有没有写的,详细的请参考http://poi.apache.org/spreadsheet/quick-guide.html

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();  
}
/**
* 边框
* @param wb
* @return
*/
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;
}
/**
* 设置文字在单元格里面的位置
* CellStyle.ALIGN_CENTER
* CellStyle.VERTICAL_CENTER
* @param cellStyle
* @param halign
* @param valign
* @return
*/
public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){
   //设置上下
   cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
   //设置左右
   cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
   return cellStyle;
}
/**
* 格式化单元格
* 如#,##0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找
* @param cellStyle
* @param fmt
* @return
*/
public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){
   //还可以用其它方法创建format
   cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
   return cellStyle;
}
/**
* 前景和背景填充的着色
* @param cellStyle
* @param bg IndexedColors.ORANGE.getIndex();
* @param fg IndexedColors.ORANGE.getIndex();
* @param fp CellStyle.SOLID_FOREGROUND
* @return
*/
public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
   //cellStyle.setFillBackgroundColor(bg);
   cellStyle.setFillForegroundColor(fg);
   cellStyle.setFillPattern(fp);
   return cellStyle;
}
/**
* 设置字体
* @param wb
* @return
*/
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("");
   }
}
}

 

还有一种传统的读法

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();  
}
/**
* 边框
* @param wb
* @return
*/
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;
}
/**
* 设置文字在单元格里面的位置
* CellStyle.ALIGN_CENTER
* CellStyle.VERTICAL_CENTER
* @param cellStyle
* @param halign
* @param valign
* @return
*/
public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){
   //设置上下
   cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
   //设置左右
   cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
   return cellStyle;
}
/**
* 格式化单元格
* 如#,##0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找
* @param cellStyle
* @param fmt
* @return
*/
public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){
   //还可以用其它方法创建format
   cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
   return cellStyle;
}
/**
* 前景和背景填充的着色
* @param cellStyle
* @param bg IndexedColors.ORANGE.getIndex();
* @param fg IndexedColors.ORANGE.getIndex();
* @param fp CellStyle.SOLID_FOREGROUND
* @return
*/
public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
   //cellStyle.setFillBackgroundColor(bg);
   cellStyle.setFillForegroundColor(fg);
   cellStyle.setFillPattern(fp);
   return cellStyle;
}
/**
* 设置字体
* @param wb
* @return
*/
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读写Excel

    以下是对使用Apache POI HSSF进行Excel读写操作的详细知识点: 1. **HSSF模型**:Apache POI使用了一种类似于Excel内部结构的模型来表示工作簿、工作表、行、单元格等元素。HSSFWorkbook代表一个工作簿,HSSFSheet...

    Apache POI HSSF和XSSF读写EXCEL总结

    Apache POI HSSF和XSSF读写EXCEL总结

    apache POI文件读写excel

    总结,Apache POI是Java开发中处理Excel文件的强大工具,涵盖了从基本的读写操作到复杂的格式设置和公式计算。通过熟练掌握Apache POI,开发者可以构建灵活且高效的Excel处理程序,满足各种业务需求。

    POI HSSF - Excel实例

    标题中的“POI HSSF”指的是Apache POI项目中的HSSF组件,这是一个Java库,用于读写Microsoft Excel(.xls)文件格式。Apache POI是一个流行的开源库,它允许开发者在Java应用程序中处理Microsoft Office文档,包括...

    Apache 1. POI HSSF和XSSF读写EXCEL总结.pdf

    。Apache 1.。POI HSSF和XSSF读写EXCEL总结.pdf

    Apache 1. POI HSSF和XSSF读写EXCEL总结.docx

    。Apache 1.。POI HSSF和XSSF读写EXCEL总结.docx

    org.apache.poi.hssf.converter,office转html所需包

    1. **HSSF API**:这是Apache POI提供用于读写旧版Excel文件的接口。HSSF模型允许开发者操作工作簿(Workbook)、工作表(Sheet)、行(Row)、单元格(Cell)等对象,进行数据的读取和写入。 2. **XSSF API**:...

    org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException

    解决POI读取EXCEL时报org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException异常

    Apache POI HSSF and XSSF 快速指南帮助文档 API poi-3.15

    Apache POI 是一个用于读取和写入 Microsoft Office 格式文件(如 Word 和 Excel)的开源 Java 库。它支持多种格式,包括旧版的二进制格式(例如 .xls 文件)和较新的基于 XML 的格式(例如 .xlsx 文件)。本指南将...

    Apache poi 操作 excel 文件压缩包

    在Java环境中,Apache POI 提供了一套API,使得开发者能够创建、修改和读取Excel文件。这个压缩包包含了Apache POI库的多个版本及其依赖库,如ooxml-schemas、xmlbeans等,用于支持对Excel文件的OOXML(Office Open ...

    Apache POI for Android

    - **读取Excel**:使用Apache POI,开发者可以打开Excel文件,访问工作簿、工作表,以及单元格中的数据。例如,通过`WorkbookFactory.create()`方法加载文件,然后遍历工作表和单元格进行数据提取。 - **写入Excel...

    Apache POI库jar文件

    Apache POI库是一个开源的Java库,可以帮助开发人员处理Microsoft Office格式的文档,例如Word文档、Excel电子表格和PowerPoint演示文稿等。以下是Apache POI库的详细介绍: 支持多种Office格式:Apache POI库支持...

    Apache POI组件操作Excel,制作报表(四)

    这篇博文将深入探讨如何使用Apache POI组件来创建、修改和读取Excel文件,以实现报表制作。Apache POI提供了HSSF(Horizontally Stored Sheets Format)用于处理.xls格式的旧版Excel文件,以及XSSF(XML Spreadsheet...

    poi读写excel+poi总结

    读取Excel文件时,首先需要使用POI的FileInputStream打开文件,然后创建与文件类型对应的Workbook实例。之后,通过Workbook获取Sheet,再通过Sheet获取Row和Cell,读取其值。 7. 遍历数据 遍历Excel文件通常需要...

    apache poi 读取Excel文件内容(2003,2007)

    这篇博文主要讲解如何使用Apache POI库来读取Excel文件的内容,无论是2003版的.XLS还是2007版及以后的.XLSX格式。 在Java编程中,Apache POI 提供了丰富的API,使得开发者可以方便地操作Excel文件。首先,我们需要...

    Apache POI资源包

    1. **读取Microsoft Office文件**:通过HSSF(Horrible Spreadsheet Format)和XSSF(XML Spreadsheet Format)API,Apache POI能够读取旧版的Excel文件(.xls)和新版本的Excel文件(.xlsx)。类似地,HWPF...

    Apache POI Excel操作

    在Java开发环境中,Apache POI 提供了丰富的API,使得开发者能够方便地在程序中创建、修改和读取Excel文件。本篇将详细介绍Apache POI在Excel操作中的应用,包括基本概念、使用步骤、关键类和方法以及实际示例。 1....

    Apache Poi Excel导出

    在Java开发中,Apache POI 提供了一种高效且灵活的方式来创建、修改和读取Excel工作簿。这篇博客“Apache Poi Excel导出”可能详细介绍了如何使用Apache POI库来生成Excel文件,特别适用于数据导出或报表生成等场景...

    Apache POI For Java Excel

    在众多处理Excel表格的Java工具中,Apache POI的HSSF组件脱颖而出,成为业界广泛认可的选择。HSSF,即Hadoop SpreadSheet Format,专门用于处理Excel 97-2003(.xls)格式的文件,其强大之处不仅体现在其功能全面性,...

    poi3.9读写EXCEL

    总结起来,Apache POI 3.9在读写Excel方面提供了强大的功能,不仅支持旧版的XLS格式,而且在处理XLSX文件时同样游刃有余。开发者可以根据需求,利用这些API创建复杂的Excel应用,如报表生成、数据分析等。

Global site tag (gtag.js) - Google Analytics