设为首页 收藏本站
查看: 648|回复: 0

[经验分享] java通过apache的POI写EXCEL

[复制链接]

尚未签到

发表于 2017-1-5 09:48:27 | 显示全部楼层 |阅读模式
  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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324103-1-1.html 上篇帖子: apache基于ssl配置weblogic(完结篇) 下篇帖子: Apache中按天分割日志(Windows)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表