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; } }
- poi-3.7-20101029.jar (1.6 MB)
- 下载次数: 7
- poi-examples-3.7-20101029.jar (257.9 KB)
- 下载次数: 5
- poi-ooxml-3.7-20101029.jar (486.6 KB)
- 下载次数: 3
- poi-scratchpad-3.7-20101029.jar (820.5 KB)
- 下载次数: 3
- poi-ooxml-schemas-3.7-20101029.jar (3.8 MB)
- 下载次数: 3
发表评论
-
CSDN表情批量下载
2014-07-24 16:55 641CSDN评论时有很多有趣的表情,可以通过程序将这些表 ... -
南京地铁票价查询
2014-07-16 16:27 959查看南京地铁各个站点间的距离 import java.io ... -
在指定路径下搜索含指定关键词的所有文件
2012-10-28 08:58 1065这个功能类似notepad++的文件搜索功能,如全词匹配“in ... -
java系统托盘
2012-07-20 22:43 935import java.awt.*; import j ... -
多线程实例
2012-07-20 22:08 725public class Class1 { publ ... -
读配置文件,传参数,打印message
2012-06-21 19:43 990为了将来维护,message内容可能需要更改 ... -
java 线程 模拟卖票
2012-05-27 13:35 972package thread; import jav ... -
java URLConnection
2012-05-27 13:36 1145package net; import java.ne ... -
java JFileChooser
2012-05-26 21:06 1037import java.io.*; import ... -
java Vector
2012-05-26 20:40 760import java.util.Vector; im ... -
java applet GUI 2D
2012-05-26 20:09 1017package applet; import java.aw ... -
java AdjustmentListener,Scrollbar的使用
2012-05-26 20:03 1003package gui.event; import j ... -
java TextListener的使用
2012-05-26 19:57 1461package gui.event; import ... -
多线程模拟测试网站并发性
2012-05-25 21:11 2877网站开发完了后,需要测试网站能支持多少用户并发访问,下面的j ... -
java 字节流使用
2012-05-21 14:04 1070package advance.test; import ... -
java全屏
2012-04-22 14:54 1044下面的代码演示了如何让JFrame全屏显示: impo ... -
JDBC-ODBC桥连接方式连接MS ACCESS,配置数据源
2012-04-22 13:59 1367下面是通过jdbc-odbc连接ms access数据库的一种 ... -
java获取系统相关信息
2012-01-12 22:12 1571下面的代码可以获取系统的环境变量,操作系统、处理器、编码和语言 ...
相关推荐
apache POI,文件读写 ,excel 对于使用apache poi 解析微软excel的一些文件
Java使用apache POI读取xlsx代码以及相关jar包
自己花了几天时间写的基于Apache POI的Java 读取excel文件,功能还是比较齐全
使用JSP+jqueryUI+java Servlet通过Apache POI实现Excel导入导出
apache poi解析Excel时报错,我将Excel打开后保存,就不会报错,我将重写的类从新打包这样问题虽然网上有解决方案,但是需要自己打包重写对应类,我已将这些步骤做好了。
Apache POI Excel操作 需要的文档,.介绍,相关jar包,maven中的配置等,比较全面
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML格式档案...
POI 删除excel列,可以设置多列,POI里怎么没有删除列的API呢
Apache POI For Java Excel.doc
java使用apache的poi处理excel
java 通过JXL架包,给excel文件添加水印,水印内容可自定义
Apache poi 是Apache在java与Office相关文档进行交互操作的一个开源包 可以轻松的能过java与word或excel进行操作
本篇文章主要介绍了Java通过apache poi生成excel实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
apache poi 导出excel、word,替换word内容,导出word-文本、图片、表格
Apache POI导入和导出Excel文件
Poi强大的操作技术方便了我们开发,Apache POI导入数据到Excel电子表格
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel ...
JAVA 生成Excel库Apache POI3.15,完整的库,已经测试过了,可以直接使用。 包含使用实例
本案例中利用Apache Poi读取Excel用法,源码注释详细基本用法!