心海恋歌 发表于 2017-1-5 09:48:27

java通过apache的POI写EXCEL

  Apache POI是Apache
软件

基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。


  HSSF - 提供读写Microsoft Excel
格式档案的功能。
  

  XSSF - 提供读写Microsoft
Excel OOXML
格式档案的功能。
  

  HWPF - 提供读写Microsoft Word
格式档案的功能。
  

  HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
  

  HDGF - 提供读写Microsoft Visio
格式档案的功能。


  


  下面的代码演示了如何创建一个excel文件,并写数据:


package excel;
import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class SummaryHSSF {
public static void main(String[] args) throws IOException {
//创建Workbook对象(这一个对象代表着对应的一个Excel文件)
//HSSFWorkbook表示以xls为后缀名的文件
Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
//获得CreationHelper对象,这个应该是一个帮助类
CreationHelper helper = wb.getCreationHelper();
//创建Sheet并给名字(表示Excel的一个Sheet)
Sheet sheet1 = wb.createSheet("sheet01");
//Row表示一行Cell表示一列
Row row = null;
Cell cell = null;
row = sheet1.createRow(1);
row.setHeightInPoints(20);
for(int r=0;r<60;r=r+2){
//获得这个sheet的第i行
row = sheet1.createRow(r);
//设置行长度自动
//row.setHeight((short)500);
row.setHeightInPoints(20);
//row.setZeroHeight(true);
for(int col=0;col<25;col++){
//设置每个sheet每一行的宽度,自动,根据需求自行确定(宽度自适应)
sheet1.autoSizeColumn(col+1, true);
//创建一个基本的样式
CellStyle cellStyle = SummaryHSSF.makeNewCellStyle(wb);
//获得这一行的每j列
cell = row.createCell(col);
if(col==0){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值(true作为单元格的值)
cell.setCellValue(true);
}else if(col==1){
//设置文字在单元格里面的位置
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//设置这个样式的格式(Format)
cellStyle = SummaryHSSF.cellFormatDecorate(helper,cellStyle, "#,##0.0000");
//先创建字体样式,并把这个样式加到单元格的字体里面
cellStyle.setFont(createFonts(wb));
//把这个样式加到单元格里面
cell.setCellStyle(cellStyle);
//给单元格设值
cell.setCellValue(new Double(2008.2008));
}else if(col==2){
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
cell.setCellStyle(cellStyle);
cell.setCellValue(helper.createRichTextString("RichString"+r+col));
}else if(col==3){
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle = SummaryHSSF.cellFormatDecorate(helper,cellStyle, "yyyy-MM-dd hh:mm:ss");
cell.setCellStyle(cellStyle);
cell.setCellValue(new Date());
}else if(col==24){
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
cellStyle.setFont(createFonts(wb));
//设置公式,无"="号
cell.setCellFormula("SUM(E"+(r+1)+":X"+(r+1)+")");
}else{
cellStyle = SummaryHSSF.alignmentDecorate(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
//背景设置
cellStyle = SummaryHSSF.fillBackgroundColorsDecorate(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);
cell.setCellStyle(cellStyle);
cell.setCellValue(new Random().nextInt(20));
}
}
}
//输出
OutputStream os = new FileOutputStream(new File("I:/SummaryHSSF_"+
(System.currentTimeMillis())+"_.xls"));
wb.write(os);
os.close();
}
/**
* 设置单元格 边框 风格(粗细,颜色)
* @param wb 这一个对象代表着对应的一个Excel文件
* @return CellStyle
*/
public static CellStyle makeNewCellStyle(Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
//设置一个单元格边框粗细(上下左右四边)
cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
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;
}
/**
* 设置文字在单元格里面的 对齐方式
* @param cellStyle
* @param halign
* @param valign decorate
* @return
*/
public static CellStyle alignmentDecorate(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 cellFormatDecorate(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 fillBackgroundColorsDecorate(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;
}
}

 
页: [1]
查看完整版本: java通过apache的POI写EXCEL