ApachePOIHSSF和XSSF读写EXCEL总结.docx
《ApachePOIHSSF和XSSF读写EXCEL总结.docx》由会员分享,可在线阅读,更多相关《ApachePOIHSSF和XSSF读写EXCEL总结.docx(10页珍藏版)》请在冰豆网上搜索。
ApachePOIHSSF和XSSF读写EXCEL总结
ApachePOIHSSF和XSSF读写EXCEL总结
HSSF是指2007年以前的,XSSF是指2007年版本以上的
这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴
还有好多没有没有写的,详细的请参考http:
//poi.apache.org/spreadsheet/quick-guide.html
还有LOG也比较好看
Java代码
publicclassSummaryHSSF{
publicstaticvoidmain(String[]args)throwsIOException{
//创建Workbook对象(这一个对象代表着对应的一个Excel文件)
//HSSFWorkbook表示以xls为后缀名的文件
Workbookwb=newHSSFWorkbook();
//获得CreationHelper对象,这个应该是一个帮助类
CreationHelperhelper=wb.getCreationHelper();
//创建Sheet并给名字(表示Excel的一个Sheet)
Sheetsheet1=wb.createSheet("HSSF_Sheet_1");
Sheetsheet2=wb.createSheet("HSSF_Sheet_2");
//Row表示一行Cell表示一列
Rowrow=null;
Cellcell=null;
for(inti=0;i<60;i=i+2){
//获得这个sheet的第i行
row=sheet1.createRow(i);
//设置行长度自动
//row.setHeight((short)500);
row.setHeightInPoints(20);
//row.setZeroHeight(true);
for(intj=0;j<25;j++){
//设置每个sheet每一行的宽度,自动,根据需求自行确定
sheet1.autoSizeColumn(j+1,true);
//创建一个基本的样式
CellStylecellStyle=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);
}elseif(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(newDouble(2008.2008));
}elseif(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));
}elseif(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(newDate());
}elseif(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);
}
}
}
//输出
OutputStreamos=newFileOutputStream(newFile("c:
//SummaryHSSF.xls"));
wb.write(os);
os.close();
}
/**
*边框
*@paramwb
*@return
*/
publicstaticCellStylecreateStyleCell(Workbookwb){
CellStylecellStyle=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());
returncellStyle;
}
/**
*设置文字在单元格里面的位置
*CellStyle.ALIGN_CENTER
*CellStyle.VERTICAL_CENTER
*@paramcellStyle
*@paramhalign
*@paramvalign
*@return
*/
publicstaticCellStylesetCellStyleAlignment(CellStylecellStyle,shorthalign,shortvalign){
//设置上下
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//设置左右
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
returncellStyle;
}
/**
*格式化单元格
*如#,##0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找
*@paramcellStyle
*@paramfmt
*@return
*/
publicstaticCellStylesetCellFormat(CreationHelperhelper,CellStylecellStyle,Stringfmt){
//还可以用其它方法创建format
cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
returncellStyle;
}
/**
*前景和背景填充的着色
*@paramcellStyle
*@parambgIndexedColors.ORANGE.getIndex();
*@paramfgIndexedColors.ORANGE.getIndex();
*@paramfpCellStyle.SOLID_FOREGROUND
*@return
*/
publicstaticCellStylesetFillBackgroundColors(CellStylecellStyle,shortbg,shortfg,shortfp){
//cellStyle.setFillBackgroundColor(bg);
cellStyle.setFillForegroundColor(fg);
cellStyle.setFillPattern(fp);
returncellStyle;
}
/**
*设置字体
*@paramwb
*@return
*/
publicstaticFontcreateFonts(Workbookwb){
//创建Font对象
Fontfont=wb.createFont();
//设置字体
font.setFontName("黑体");
//着色
font.setColor(HSSFColor.BLUE.index);
//斜体
font.setItalic(true);
//字体大小
font.setFontHeight((short)300);
returnfont;
}
}
publicclassSummaryHSSF{
publicstaticvoidmain(String[]args)throwsIOException{
//创建Workbook对象(这一个对象代表着对应的一个Excel文件)
//HSSFWorkbook表示以xls为后缀名的文件
Workbookwb=newHSSFWorkbook();
//获得CreationHelper对象,这个应该是一个帮助类
CreationHelperhelper=wb.getCreationHelper();
//创建Sheet并给名字(表示Excel的一个Sheet)
Sheetsheet1=wb.createSheet("HSSF_Sheet_1");
Sheetsheet2=wb.createSheet("HSSF_Sheet_2");
//Row表示一行Cell表示一列
Rowrow=null;
Cellcell=null;
for(inti=0;i<60;i=i+2){
//获得这个sheet的第i行
row=sheet1.createRow(i);
//设置行长度自动
//row.setHeight((short)500);
row.setHeightInPoints(20);
//row.setZeroHeight(true);
for(intj=0;j<25;j++){
//设置每个sheet每一行的宽度,自动,根据需求自行确定
sheet1.autoSizeColumn(j+1,true);
//创建一个基本的样式
CellStylecellStyle=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);
}elseif(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(newDouble(2008.2008));
}elseif(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));
}elseif(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(newDate());
}elseif(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);
}
}
}
//输出
OutputStreamos=newFileOutputStream(newFile("c:
//SummaryHSSF.xls"));
wb.write(os);
os.close();
}
/**
*边框
*@paramwb
*@return
*/
publicstaticCellStylecreateStyleCell(Workbookwb){
CellStylecellStyle=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());
returncellStyle;
}
/**
*设置文字在单元格里面的位置
*CellStyle.ALIGN_CENTER
*CellStyle.VERTICAL_CENTER
*@paramcellStyle
*@paramhalign
*@paramvalign
*@return
*/
publicstaticCellStylesetCellStyleAlignment(CellStylecellStyle,shorthalign,shortvalign){
//设置上下
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
//设置左右
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
returncellStyle;
}
/**
*格式化单元格
*如#,##0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找
*@paramcellStyle
*@paramfmt
*@return
*/
publicstaticCellStylesetCellFormat(CreationHelperhelper,CellStylecellStyle,Stringfmt){
//还可以用其它方法创建format
cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
returncellStyle;
}
/**
*前景和背景填充的着色
*@paramcellStyle
*@parambgIndexedColors.ORANGE.getIndex();
*@paramfgIndexedColors.ORANGE.getIndex();
*@paramfpCellStyle.SOLID_FOREGROUND
*@return
*/
publicstaticCellStylesetFillBackgroundColors(CellStylecellStyle,shortbg,shortfg,shortfp){
//cellStyle.setFillBackgroundColor(bg);
cellStyle.setFillForegroundColor(fg);
cellStyle.setFillPattern(fp);
returncellStyle;
}
/**
*设置字体
*@paramwb
*@return
*/
publicstaticFontcreateFonts(Workbookwb){
//创建Font对象
Fontfont=wb.createFont();
//设置字体
font.setFontName("黑体");
//着色
font.setColor(HSSFColor.BLUE.index);
//斜体
font.setItalic(true);
//字体大小
font.setFontHeight((short)300);
returnfont;
}
}
读取Excel文件
Java代码
publicclassReadExcel{
publicstaticvoidmain(String[]args)throwsExce