- 浏览: 575185 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (174)
- JBPM (3)
- WWF (0)
- JavaScript (11)
- J2EE (40)
- OperationSystem (11)
- 数据库 (12)
- CSS (1)
- Ajax (2)
- J2SE (30)
- Tools (10)
- 服务器中间件 (3)
- 异常 (0)
- Flex (5)
- jQuery (11)
- html (9)
- Ejb (1)
- HTML5 Shiv–让该死的IE系列支持HTML5吧 (1)
- Spring (9)
- Quartz (3)
- log4j (1)
- maven (1)
- cpdetector (1)
- JSON (1)
- log4jdbc (1)
- asm (8)
- FusionCharts (1)
- jqplot (1)
- highcharts (1)
- excanvas (1)
- html5 (1)
- jpcap介绍 (1)
- weblogic (3)
- URLURLClassLoader (0)
- URLClassLoader (1)
- ant (2)
- ivy (2)
- nexus (1)
- IT (0)
- LoadRunner (1)
- SCSS (1)
- ruby (1)
- webstorm (1)
- typescript (1)
- Jboss7 (1)
- wildfly (1)
- oracle (5)
- esb (0)
- dubbo (2)
- zookeeper (3)
- eclipse (1)
- Android (2)
- Studio (1)
- Google (1)
- 微信 (1)
- 企业号 (1)
- Linux (13)
- Oracle12c (1)
- Hadoop (1)
- InletexEMC (1)
- Windows (1)
- Netty (3)
- Marshalling (2)
- Protobuf (1)
- gcc (1)
- Git (1)
- GitLab (1)
- shell (2)
- java (3)
- Spring4 (1)
- hibernate4 (1)
- postgresql (1)
- ApacheServer (2)
- Tomcat (2)
- ApacheHttpServer (2)
- realvnc (1)
- redhat (7)
- vncviewer (1)
- LVS (4)
- LVS-DR (1)
- RedHat6.5 (5)
- LVS-NAT (1)
- LVS-IPTUNNEL (2)
- LVS-TUN (1)
- keepalived (2)
- yum (1)
- iso (1)
- VMware (1)
- redhat5 (1)
- ha (1)
- nginx (2)
- proguard (1)
- Mat (1)
- DTFJ (1)
- axis2 (1)
- web service (1)
- centos (1)
- random (1)
- urandom (1)
- apache (1)
- IBM (1)
- cve (1)
- 漏洞 (1)
- JDBC (1)
- DataSource (1)
- jdk (1)
- tuxedo (2)
- wtc (1)
最新评论
-
skying007:
好资料,谢谢分享给啊
FusionCharts在服务器端导出图片(J2EE版) -
cgnnzg:
大神好 可以发一份源码给我学习么 多谢了 978241085 ...
springmvc+dubbo+zookeeper -
jifengjianhao:
求源码:854606899@qq.com
springmvc+dubbo+zookeeper -
wdloyeu:
shihuan8@163.com邮箱网盘在哪,没找到。能给份源 ...
Java Socket长连接示例代码 -
huangshangyuanji:
求代码:45613032@qq.com
springmvc+dubbo+zookeeper
一.POI简介
Jakarta POI 是apache的子项目,目标是处理ole2对象。它提供了一组操纵Windows文档的Java API
目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
二.HSSF概况
HSSF 是Horrible SpreadSheet Format的缩写,也即“讨厌的电子表格格式”。 也许HSSF的名字有点滑稽,就本质而言它是一个非常严肃、正规的API。通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。
HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多。
三.开始编码
1 . 准备工作
要求:JDK 1.4+POI开发包
可以到 http://www.apache.org/dyn/closer.cgi/jakarta/poi/ 最新的POI工具包
2 . EXCEL 结构
HSSFWorkbook excell 文档对象介绍
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
4 . 可参考文档
POI 主页:http://jakarta.apache.org/poi/,
初学者如何快速上手使用POI HSSF
http://jakarta.apache.org/poi/hssf/quick-guide.html 。
代码例子 http://blog.java-cn.com/user1/6749/archives/2005/18347.html
里面有很多例子代码,可以很方便上手。
四.使用心得
POI HSSF 的usermodel包把Excel文件映射成我们熟悉的结构,诸如Workbook、Sheet、Row、Cell等,它把整个结构以一组对象的形式保存在内存之中,便于理解,操作方便,基本上能够满足我们的要求,所以说这个一个不错的选择。
1.创建工作簿 (WORKBOOK)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
3.wb.write(fileOut);
4.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
2.创建工作表(SHEET)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.HSSFSheet sheet2 = wb.createSheet("second sheet");
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
3.创建单元格(CELL)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a value in it.
6.HSSFCell cell = row.createCell((short)0);
7.cell.setCellValue(1);
8.// Or do it on one line.
9.row.createCell((short)1).setCellValue(1.2);
10.row.createCell((short)2).setCellValue("This is a string");
11.row.createCell((short)3).setCellValue(true);
12.// Write the output to a file
13.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
14.wb.write(fileOut);
15.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
4.创建指定单元格式的单元格
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a date value in it. The first cell is not styled
6.// as a date.
7.HSSFCell cell = row.createCell((short)0);
8.cell.setCellValue(new Date());
9.// we style the second cell as a date (and time). It is important to
10.// create a new cell style from the workbook otherwise you can end up
11.// modifying the built in style and effecting not only this cell but other cells.
12.HSSFCellStyle cellStyle = wb.createCellStyle();
13.cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
14.cell = row.createCell((short)1);
15.cell.setCellValue(new Date());
16.cell.setCellStyle(cellStyle);
17.// Write the output to a file
18.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
19.wb.write(fileOut);
20.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a date value in it. The first cell is not styled
// as a date.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
5. 单元格的不同格式
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short)2);
4.row.createCell((short) 0).setCellValue(1.1);
5.row.createCell((short) 1).setCellValue(new Date());
6.row.createCell((short) 2).setCellValue("a string");
7.row.createCell((short) 3).setCellValue(true);
8.row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
9.// Write the output to a file
10.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
11.wb.write(fileOut);
12.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)2);
row.createCell((short) 0).setCellValue(1.1);
row.createCell((short) 1).setCellValue(new Date());
row.createCell((short) 2).setCellValue("a string");
row.createCell((short) 3).setCellValue(true);
row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
6.单元格的不通对齐方式
Java代码
1.public static void main(String[] args)
2.throws IOException
3.{
4.HSSFWorkbook wb = new HSSFWorkbook();
5.HSSFSheet sheet = wb.createSheet("new sheet");
6.HSSFRow row = sheet.createRow((short) 2);
7.createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
8.createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
9.createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
10.createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
11.createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
12.createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
13.createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
14.// Write the output to a file
15.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
16.wb.write(fileOut);
17.fileOut.close();
18.}
19./**
20.* Creates a cell and aligns it a certain way.
21.*
22.* @param wb the workbook
23.* @param row the row to create the cell in
24.* @param column the column number to create the cell in
25.* @param align the alignment for the cell.
26.*/
27.private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
28.{
29.HSSFCell cell = row.createCell(column);
30.cell.setCellValue("Align It");
31.HSSFCellStyle cellStyle = wb.createCellStyle();
32.cellStyle.setAlignment(align);
33.cell.setCellStyle(cellStyle);
34.}
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param align the alignment for the cell.
*/
private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
{
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cell.setCellStyle(cellStyle);
}
7.单元格的边框设置
Java代码
1.//Working with borders
2.HSSFWorkbook wb = new HSSFWorkbook();
3.HSSFSheet sheet = wb.createSheet("new sheet");
4.// Create a row and put some cells in it. Rows are 0 based.
5.HSSFRow row = sheet.createRow((short) 1);
6.// Create a cell and put a value in it.
7.HSSFCell cell = row.createCell((short) 1);
8.cell.setCellValue(4);
9.// Style the cell with borders all around.
10.HSSFCellStyle style = wb.createCellStyle();
11.style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
12.style.setBottomBorderColor(HSSFColor.BLACK.index);
13.style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
14.style.setLeftBorderColor(HSSFColor.GREEN.index);
15.style.setBorderRight(HSSFCellStyle.BORDER_THIN);
16.style.setRightBorderColor(HSSFColor.BLUE.index);
17.style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
18.style.setTopBorderColor(HSSFColor.BLACK.index);
19.cell.setCellStyle(style);
20.// Write the output to a file
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
//Working with borders
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.BLACK.index);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
8.填充和颜色设置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Aqua background
6.HSSFCellStyle style = wb.createCellStyle();
7.style.setFillBackgroundColor(HSSFColor.AQUA.index);
8.style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
9.HSSFCell cell = row.createCell((short) 1);
10.cell.setCellValue("X");
11.cell.setCellStyle(style);
12.// Orange "foreground", foreground being the fill foreground not the font color.
13.style = wb.createCellStyle();
14.style.setFillForegroundColor(HSSFColor.ORANGE.index);
15.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
16.cell = row.createCell((short) 2);
17.cell.setCellValue("X");
18.cell.setCellStyle(style);
19.// Write the output to a file
20.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
21.wb.write(fileOut);
22.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
9.合并单元格操作
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short) 1);
4.HSSFCell cell = row.createCell((short) 1);
5.cell.setCellValue("This is a test of merging");
6.sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
7.// Write the output to a file
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 1);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
9.1合并单元格边框的解决办法
Java代码
1.private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
2. int toprowNum = region.getRowFrom();
3. for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
4. HSSFRow row = HSSFCellUtil.getRow(i, sheet);
5. for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
6. HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
7. cell.setCellStyle(cs);
8. }
9. }
10.}
private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
int toprowNum = region.getRowFrom();
for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
HSSFRow row = HSSFCellUtil.getRow(i, sheet);
for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
cell.setCellStyle(cs);
}
}
}
10.字体设置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Create a new font and alter it.
6.HSSFFont font = wb.createFont();
7.font.setFontHeightInPoints((short)24);
8.font.setFontName("Courier New");
9.font.setItalic(true);
10.font.setStrikeout(true);
11.// Fonts are set into a style so create a new one to use.
12.HSSFCellStyle style = wb.createCellStyle();
13.style.setFont(font);
14.// Create a cell and put a value in it.
15.HSSFCell cell = row.createCell((short) 1);
16.cell.setCellValue("This is a test of fonts");
17.cell.setCellStyle(style);
18.// Write the output to a file
19.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
20.wb.write(fileOut);
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a new font and alter it.
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
11.自定义颜色
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet();
3.HSSFRow row = sheet.createRow((short) 0);
4.HSSFCell cell = row.createCell((short) 0);
5.cell.setCellValue("Default Palette");
6.//apply some colors from the standard palette,
7.// as in the previous examples.
8.//we'll use red text on a lime background
9.HSSFCellStyle style = wb.createCellStyle();
10.style.setFillForegroundColor(HSSFColor.LIME.index);
11.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
12.HSSFFont font = wb.createFont();
13.font.setColor(HSSFColor.RED.index);
14.style.setFont(font);
15.cell.setCellStyle(style);
16.//save with the default palette
17.FileOutputStream out = new FileOutputStream("default_palette.xls");
18.wb.write(out);
19.out.close();
20.//now, let's replace RED and LIME in the palette
21.// with a more attractive combination
22.// (lovingly borrowed from freebsd.org)
23.cell.setCellValue("Modified Palette");
24.//creating a custom palette for the workbook
25.HSSFPalette palette = wb.getCustomPalette();
26.//replacing the standard red with freebsd.org red
27.palette.setColorAtIndex(HSSFColor.RED.index,
28.(byte) 153, //RGB red (0-255)
29.(byte) 0, //RGB green
30.(byte) 0 //RGB blue
31.);
32.//replacing lime with freebsd.org gold
33.palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
34.//save with the modified palette
35.// note that wherever we have previously used RED or LIME, the
36.// new colors magically appear
37.out = new FileOutputStream("modified_palette.xls");
38.wb.write(out);
39.out.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
//replacing lime with freebsd.org gold
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();
12.读和重写EXCEL文件
Java代码
1.POIFSFileSystem fs =
2.new POIFSFileSystem(new FileInputStream("workbook.xls"));
3.HSSFWorkbook wb = new HSSFWorkbook(fs);
4.HSSFSheet sheet = wb.getSheetAt(0);
5.HSSFRow row = sheet.getRow(2);
6.HSSFCell cell = row.getCell((short)3);
7.if (cell == null)
8.cell = row.createCell((short)3);
9.cell.setCellType(HSSFCell.CELL_TYPE_STRING);
10.cell.setCellValue("a test");
11.// Write the output to a file
12.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
13.wb.write(fileOut);
14.fileOut.close();
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
13.在EXCEL单元格中使用自动换行
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet s = wb.createSheet();
3.HSSFRow r = null;
4.HSSFCell c = null;
5.HSSFCellStyle cs = wb.createCellStyle();
6.HSSFFont f = wb.createFont();
7.HSSFFont f2 = wb.createFont();
8.cs = wb.createCellStyle();
9.cs.setFont( f2 );
10.//Word Wrap MUST be turned on
11.cs.setWrapText( true );
12.r = s.createRow( (short) 2 );
13.r.setHeight( (short) 0x349 );
14.c = r.createCell( (short) 2 );
15.c.setCellType( HSSFCell.CELL_TYPE_STRING );
16.c.setCellValue( "Use \n with word wrap on to create a new line" );
17.c.setCellStyle( cs );
18.s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
19.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
20.wb.write( fileOut );
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
cs = wb.createCellStyle();
cs.setFont( f2 );
//Word Wrap MUST be turned on
cs.setWrapText( true );
r = s.createRow( (short) 2 );
r.setHeight( (short) 0x349 );
c = r.createCell( (short) 2 );
c.setCellType( HSSFCell.CELL_TYPE_STRING );
c.setCellValue( "Use \n with word wrap on to create a new line" );
c.setCellStyle( cs );
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();
14.数字格式自定义
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFCellStyle style;
4.HSSFDataFormat format = wb.createDataFormat();
5.HSSFRow row;
6.HSSFCell cell;
7.short rowNum = 0;
8.short colNum = 0;
9.row = sheet.createRow(rowNum++);
10.cell = row.createCell(colNum);
11.cell.setCellValue(11111.25);
12.style = wb.createCellStyle();
13.style.setDataFormat(format.getFormat("0.0"));
14.cell.setCellStyle(style);
15.row = sheet.createRow(rowNum++);
16.cell = row.createCell(colNum);
17.cell.setCellValue(11111.25);
18.style = wb.createCellStyle();
19.style.setDataFormat(format.getFormat("#,##0.0000"));
20.cell.setCellStyle(style);
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFCellStyle style;
HSSFDataFormat format = wb.createDataFormat();
HSSFRow row;
HSSFCell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
15.调整工作单位置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFPrintSetup ps = sheet.getPrintSetup();
4.sheet.setAutobreaks(true);
5.ps.setFitHeight((short)1);
6.ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setAutobreaks(true);
ps.setFitHeight((short)1);
ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
16.设置打印区域
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("Sheet1");
3.wb.setPrintArea(0, "$A$1:$C$2");
4.//sets the print area for the first sheet
5.//Alternatively:
6.//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
7.// Create various cells and rows for spreadsheet.
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
wb.setPrintArea(0, "$A$1:$C$2");
//sets the print area for the first sheet
//Alternatively:
//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
17.标注脚注
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFFooter footer = sheet.getFooter()
4.footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
5.
6.// Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter()
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
18.使用方便的内部提供的函数
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet( "new sheet" );
3.// Create a merged region
4.HSSFRow row = sheet1.createRow( (short) 1 );
5.HSSFRow row2 = sheet1.createRow( (short) 2 );
6.HSSFCell cell = row.createCell( (short) 1 );
7.cell.setCellValue( "This is a test of merging" );
8.Region region = new Region( 1, (short) 1, 4, (short) 4 );
9.sheet1.addMergedRegion( region );
10.// Set the border and border colors.
11.final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
12.HSSFRegionUtil.setBorderBottom( borderMediumDashed,
13.region, sheet1, wb );
14.HSSFRegionUtil.setBorderTop( borderMediumDashed,
15.region, sheet1, wb );
16.HSSFRegionUtil.setBorderLeft( borderMediumDashed,
17.region, sheet1, wb );
18.HSSFRegionUtil.setBorderRight( borderMediumDashed,
19.region, sheet1, wb );
20.HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
21.HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
22.HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
23.HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
24.// Shows some usages of HSSFCellUtil
25.HSSFCellStyle style = wb.createCellStyle();
26.style.setIndention((short)4);
27.HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
28.HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
29.HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
30.// Write out the workbook
31.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
32.wb.write( fileOut );
33.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet( "new sheet" );
// Create a merged region
HSSFRow row = sheet1.createRow( (short) 1 );
HSSFRow row2 = sheet1.createRow( (short) 2 );
HSSFCell cell = row.createCell( (short) 1 );
cell.setCellValue( "This is a test of merging" );
Region region = new Region( 1, (short) 1, 4, (short) 4 );
sheet1.addMergedRegion( region );
// Set the border and border colors.
final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
HSSFRegionUtil.setBorderBottom( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderTop( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderLeft( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderRight( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
// Shows some usages of HSSFCellUtil
HSSFCellStyle style = wb.createCellStyle();
style.setIndention((short)4);
HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
// Write out the workbook
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();
19.在工作单中移动行,调整行的上下位置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.// Create various cells and rows for spreadsheet.
4.// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
5.sheet.shiftRows(5, 10, -5);
6.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
7.wb.write(fileOut);
8.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
// Create various cells and rows for spreadsheet.
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
sheet.shiftRows(5, 10, -5);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
20.选种指定的工作单
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.sheet.setSelected(true);
4.// Create various cells and rows for spreadsheet.
5.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
6.wb.write(fileOut);
7.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
sheet.setSelected(true);
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
21.工作单的放大缩小
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.sheet1.setZoom(3,4); // 75 percent magnification
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
22.头注和脚注
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFHeader header = sheet.getHeader();
4.header.setCenter("Center Header");
5.header.setLeft("Left Header");
6.header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
7.HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFHeader header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft("Left Header");
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
23.图片的使用
Java代码
1.//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
2.ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
3.BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
4.ImageIO.write(bufferImg,"jpg",byteArrayOut);
5.
6.//读进一个excel模版
7.FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
8.fs = new POIFSFileSystem(fos);
9.//创建一个工作薄
10.HSSFWorkbook wb = new HSSFWorkbook(fs);
11.HSSFSheet sheet = wb.getSheetAt(0);
12.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
13.HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
14.patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
//读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
fs = new POIFSFileSystem(fos);
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//-------------------------------以上实例代码均来自官方网站
//-------------------------------POI中使用的颜色是用颜色索引来实现,如下:
/*
* 颜色对照表 数字代表颜色索引
8: BLACK
60: BROWN
59: OLIVE_GREEN
58: DARK_GREEN
56: DARK_TEAL
18: DARK_BLUE
32: DARK_BLUE
62: INDIGO
63: GREY_80_PERCENT
53: ORANGE
19: DARK_YELLOW
17: GREEN
21: TEAL
38: TEAL
12: BLUE
39: BLUE
54: BLUE_GREY
23: GREY_50_PERCENT
10: RED
52: LIGHT_ORANGE
50: LIME
57: SEA_GREEN
49: AQUA
48: LIGHT_BLUE
20: VIOLET
36: VIOLET
55: GREY_40_PERCENT
14: PINK
33: PINK
51: GOLD
13: YELLOW
34: YELLOW
11: BRIGHT_GREEN
35: BRIGHT_GREEN
15: TURQUOISE
35: TURQUOISE
16: DARK_RED
37: DARK_RED
40: SKY_BLUE
61: PLUM
25: PLUM
22: GREY_25_PERCENT
45: ROSE
43: LIGHT_YELLOW
42: LIGHT_GREEN
41: LIGHT_TURQUOISE
27:LIGHT_TURQUOISE
44: PALE_BLUE
46: LAVENDER
9: WHITE
24: CORNFLOWER_BLUE
26: LEMON_CHIFFON
25: MAROON
28: ORCHID
29: CORAL
30: ROYAL_BLUE
31: LIGHT_CORNFLOWER_BLUE
*/
//----------------------------------------------------你可以按上面的方法来自定义颜色
/*
* 自定义颜色,去掉注释,贴加,其他则查看颜色对照表
HSSFPalette palette = this.getCustomPalette();
palette.setColorAtIndex(idx,
i, //RGB red (0-255)
j, //RGB green
k //RGB blue
);
*/
资源来自:http://tomgreenintel.iteye.com/blog/204106
Jakarta POI 是apache的子项目,目标是处理ole2对象。它提供了一组操纵Windows文档的Java API
目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
二.HSSF概况
HSSF 是Horrible SpreadSheet Format的缩写,也即“讨厌的电子表格格式”。 也许HSSF的名字有点滑稽,就本质而言它是一个非常严肃、正规的API。通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。
HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多。
三.开始编码
1 . 准备工作
要求:JDK 1.4+POI开发包
可以到 http://www.apache.org/dyn/closer.cgi/jakarta/poi/ 最新的POI工具包
2 . EXCEL 结构
HSSFWorkbook excell 文档对象介绍
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
4 . 可参考文档
POI 主页:http://jakarta.apache.org/poi/,
初学者如何快速上手使用POI HSSF
http://jakarta.apache.org/poi/hssf/quick-guide.html 。
代码例子 http://blog.java-cn.com/user1/6749/archives/2005/18347.html
里面有很多例子代码,可以很方便上手。
四.使用心得
POI HSSF 的usermodel包把Excel文件映射成我们熟悉的结构,诸如Workbook、Sheet、Row、Cell等,它把整个结构以一组对象的形式保存在内存之中,便于理解,操作方便,基本上能够满足我们的要求,所以说这个一个不错的选择。
1.创建工作簿 (WORKBOOK)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
3.wb.write(fileOut);
4.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
2.创建工作表(SHEET)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.HSSFSheet sheet2 = wb.createSheet("second sheet");
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
3.创建单元格(CELL)
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a value in it.
6.HSSFCell cell = row.createCell((short)0);
7.cell.setCellValue(1);
8.// Or do it on one line.
9.row.createCell((short)1).setCellValue(1.2);
10.row.createCell((short)2).setCellValue("This is a string");
11.row.createCell((short)3).setCellValue(true);
12.// Write the output to a file
13.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
14.wb.write(fileOut);
15.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
4.创建指定单元格式的单元格
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a date value in it. The first cell is not styled
6.// as a date.
7.HSSFCell cell = row.createCell((short)0);
8.cell.setCellValue(new Date());
9.// we style the second cell as a date (and time). It is important to
10.// create a new cell style from the workbook otherwise you can end up
11.// modifying the built in style and effecting not only this cell but other cells.
12.HSSFCellStyle cellStyle = wb.createCellStyle();
13.cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
14.cell = row.createCell((short)1);
15.cell.setCellValue(new Date());
16.cell.setCellStyle(cellStyle);
17.// Write the output to a file
18.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
19.wb.write(fileOut);
20.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)0);
// Create a cell and put a date value in it. The first cell is not styled
// as a date.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
5. 单元格的不同格式
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short)2);
4.row.createCell((short) 0).setCellValue(1.1);
5.row.createCell((short) 1).setCellValue(new Date());
6.row.createCell((short) 2).setCellValue("a string");
7.row.createCell((short) 3).setCellValue(true);
8.row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
9.// Write the output to a file
10.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
11.wb.write(fileOut);
12.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)2);
row.createCell((short) 0).setCellValue(1.1);
row.createCell((short) 1).setCellValue(new Date());
row.createCell((short) 2).setCellValue("a string");
row.createCell((short) 3).setCellValue(true);
row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
6.单元格的不通对齐方式
Java代码
1.public static void main(String[] args)
2.throws IOException
3.{
4.HSSFWorkbook wb = new HSSFWorkbook();
5.HSSFSheet sheet = wb.createSheet("new sheet");
6.HSSFRow row = sheet.createRow((short) 2);
7.createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
8.createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
9.createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
10.createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
11.createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
12.createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
13.createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
14.// Write the output to a file
15.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
16.wb.write(fileOut);
17.fileOut.close();
18.}
19./**
20.* Creates a cell and aligns it a certain way.
21.*
22.* @param wb the workbook
23.* @param row the row to create the cell in
24.* @param column the column number to create the cell in
25.* @param align the alignment for the cell.
26.*/
27.private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
28.{
29.HSSFCell cell = row.createCell(column);
30.cell.setCellValue("Align It");
31.HSSFCellStyle cellStyle = wb.createCellStyle();
32.cellStyle.setAlignment(align);
33.cell.setCellStyle(cellStyle);
34.}
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param align the alignment for the cell.
*/
private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
{
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cell.setCellStyle(cellStyle);
}
7.单元格的边框设置
Java代码
1.//Working with borders
2.HSSFWorkbook wb = new HSSFWorkbook();
3.HSSFSheet sheet = wb.createSheet("new sheet");
4.// Create a row and put some cells in it. Rows are 0 based.
5.HSSFRow row = sheet.createRow((short) 1);
6.// Create a cell and put a value in it.
7.HSSFCell cell = row.createCell((short) 1);
8.cell.setCellValue(4);
9.// Style the cell with borders all around.
10.HSSFCellStyle style = wb.createCellStyle();
11.style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
12.style.setBottomBorderColor(HSSFColor.BLACK.index);
13.style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
14.style.setLeftBorderColor(HSSFColor.GREEN.index);
15.style.setBorderRight(HSSFCellStyle.BORDER_THIN);
16.style.setRightBorderColor(HSSFColor.BLUE.index);
17.style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
18.style.setTopBorderColor(HSSFColor.BLACK.index);
19.cell.setCellStyle(style);
20.// Write the output to a file
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
//Working with borders
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.BLACK.index);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
8.填充和颜色设置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Aqua background
6.HSSFCellStyle style = wb.createCellStyle();
7.style.setFillBackgroundColor(HSSFColor.AQUA.index);
8.style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
9.HSSFCell cell = row.createCell((short) 1);
10.cell.setCellValue("X");
11.cell.setCellStyle(style);
12.// Orange "foreground", foreground being the fill foreground not the font color.
13.style = wb.createCellStyle();
14.style.setFillForegroundColor(HSSFColor.ORANGE.index);
15.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
16.cell = row.createCell((short) 2);
17.cell.setCellValue("X");
18.cell.setCellStyle(style);
19.// Write the output to a file
20.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
21.wb.write(fileOut);
22.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
9.合并单元格操作
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short) 1);
4.HSSFCell cell = row.createCell((short) 1);
5.cell.setCellValue("This is a test of merging");
6.sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
7.// Write the output to a file
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 1);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
9.1合并单元格边框的解决办法
Java代码
1.private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
2. int toprowNum = region.getRowFrom();
3. for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
4. HSSFRow row = HSSFCellUtil.getRow(i, sheet);
5. for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
6. HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
7. cell.setCellStyle(cs);
8. }
9. }
10.}
private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
int toprowNum = region.getRowFrom();
for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
HSSFRow row = HSSFCellUtil.getRow(i, sheet);
for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
cell.setCellStyle(cs);
}
}
}
10.字体设置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Create a new font and alter it.
6.HSSFFont font = wb.createFont();
7.font.setFontHeightInPoints((short)24);
8.font.setFontName("Courier New");
9.font.setItalic(true);
10.font.setStrikeout(true);
11.// Fonts are set into a style so create a new one to use.
12.HSSFCellStyle style = wb.createCellStyle();
13.style.setFont(font);
14.// Create a cell and put a value in it.
15.HSSFCell cell = row.createCell((short) 1);
16.cell.setCellValue("This is a test of fonts");
17.cell.setCellStyle(style);
18.// Write the output to a file
19.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
20.wb.write(fileOut);
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a new font and alter it.
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
11.自定义颜色
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet();
3.HSSFRow row = sheet.createRow((short) 0);
4.HSSFCell cell = row.createCell((short) 0);
5.cell.setCellValue("Default Palette");
6.//apply some colors from the standard palette,
7.// as in the previous examples.
8.//we'll use red text on a lime background
9.HSSFCellStyle style = wb.createCellStyle();
10.style.setFillForegroundColor(HSSFColor.LIME.index);
11.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
12.HSSFFont font = wb.createFont();
13.font.setColor(HSSFColor.RED.index);
14.style.setFont(font);
15.cell.setCellStyle(style);
16.//save with the default palette
17.FileOutputStream out = new FileOutputStream("default_palette.xls");
18.wb.write(out);
19.out.close();
20.//now, let's replace RED and LIME in the palette
21.// with a more attractive combination
22.// (lovingly borrowed from freebsd.org)
23.cell.setCellValue("Modified Palette");
24.//creating a custom palette for the workbook
25.HSSFPalette palette = wb.getCustomPalette();
26.//replacing the standard red with freebsd.org red
27.palette.setColorAtIndex(HSSFColor.RED.index,
28.(byte) 153, //RGB red (0-255)
29.(byte) 0, //RGB green
30.(byte) 0 //RGB blue
31.);
32.//replacing lime with freebsd.org gold
33.palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
34.//save with the modified palette
35.// note that wherever we have previously used RED or LIME, the
36.// new colors magically appear
37.out = new FileOutputStream("modified_palette.xls");
38.wb.write(out);
39.out.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
//replacing lime with freebsd.org gold
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();
12.读和重写EXCEL文件
Java代码
1.POIFSFileSystem fs =
2.new POIFSFileSystem(new FileInputStream("workbook.xls"));
3.HSSFWorkbook wb = new HSSFWorkbook(fs);
4.HSSFSheet sheet = wb.getSheetAt(0);
5.HSSFRow row = sheet.getRow(2);
6.HSSFCell cell = row.getCell((short)3);
7.if (cell == null)
8.cell = row.createCell((short)3);
9.cell.setCellType(HSSFCell.CELL_TYPE_STRING);
10.cell.setCellValue("a test");
11.// Write the output to a file
12.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
13.wb.write(fileOut);
14.fileOut.close();
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
13.在EXCEL单元格中使用自动换行
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet s = wb.createSheet();
3.HSSFRow r = null;
4.HSSFCell c = null;
5.HSSFCellStyle cs = wb.createCellStyle();
6.HSSFFont f = wb.createFont();
7.HSSFFont f2 = wb.createFont();
8.cs = wb.createCellStyle();
9.cs.setFont( f2 );
10.//Word Wrap MUST be turned on
11.cs.setWrapText( true );
12.r = s.createRow( (short) 2 );
13.r.setHeight( (short) 0x349 );
14.c = r.createCell( (short) 2 );
15.c.setCellType( HSSFCell.CELL_TYPE_STRING );
16.c.setCellValue( "Use \n with word wrap on to create a new line" );
17.c.setCellStyle( cs );
18.s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
19.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
20.wb.write( fileOut );
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
cs = wb.createCellStyle();
cs.setFont( f2 );
//Word Wrap MUST be turned on
cs.setWrapText( true );
r = s.createRow( (short) 2 );
r.setHeight( (short) 0x349 );
c = r.createCell( (short) 2 );
c.setCellType( HSSFCell.CELL_TYPE_STRING );
c.setCellValue( "Use \n with word wrap on to create a new line" );
c.setCellStyle( cs );
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();
14.数字格式自定义
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFCellStyle style;
4.HSSFDataFormat format = wb.createDataFormat();
5.HSSFRow row;
6.HSSFCell cell;
7.short rowNum = 0;
8.short colNum = 0;
9.row = sheet.createRow(rowNum++);
10.cell = row.createCell(colNum);
11.cell.setCellValue(11111.25);
12.style = wb.createCellStyle();
13.style.setDataFormat(format.getFormat("0.0"));
14.cell.setCellStyle(style);
15.row = sheet.createRow(rowNum++);
16.cell = row.createCell(colNum);
17.cell.setCellValue(11111.25);
18.style = wb.createCellStyle();
19.style.setDataFormat(format.getFormat("#,##0.0000"));
20.cell.setCellStyle(style);
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFCellStyle style;
HSSFDataFormat format = wb.createDataFormat();
HSSFRow row;
HSSFCell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
15.调整工作单位置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFPrintSetup ps = sheet.getPrintSetup();
4.sheet.setAutobreaks(true);
5.ps.setFitHeight((short)1);
6.ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setAutobreaks(true);
ps.setFitHeight((short)1);
ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
16.设置打印区域
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("Sheet1");
3.wb.setPrintArea(0, "$A$1:$C$2");
4.//sets the print area for the first sheet
5.//Alternatively:
6.//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
7.// Create various cells and rows for spreadsheet.
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
wb.setPrintArea(0, "$A$1:$C$2");
//sets the print area for the first sheet
//Alternatively:
//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
17.标注脚注
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFFooter footer = sheet.getFooter()
4.footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
5.
6.// Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter()
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
18.使用方便的内部提供的函数
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet( "new sheet" );
3.// Create a merged region
4.HSSFRow row = sheet1.createRow( (short) 1 );
5.HSSFRow row2 = sheet1.createRow( (short) 2 );
6.HSSFCell cell = row.createCell( (short) 1 );
7.cell.setCellValue( "This is a test of merging" );
8.Region region = new Region( 1, (short) 1, 4, (short) 4 );
9.sheet1.addMergedRegion( region );
10.// Set the border and border colors.
11.final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
12.HSSFRegionUtil.setBorderBottom( borderMediumDashed,
13.region, sheet1, wb );
14.HSSFRegionUtil.setBorderTop( borderMediumDashed,
15.region, sheet1, wb );
16.HSSFRegionUtil.setBorderLeft( borderMediumDashed,
17.region, sheet1, wb );
18.HSSFRegionUtil.setBorderRight( borderMediumDashed,
19.region, sheet1, wb );
20.HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
21.HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
22.HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
23.HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
24.// Shows some usages of HSSFCellUtil
25.HSSFCellStyle style = wb.createCellStyle();
26.style.setIndention((short)4);
27.HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
28.HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
29.HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
30.// Write out the workbook
31.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
32.wb.write( fileOut );
33.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet( "new sheet" );
// Create a merged region
HSSFRow row = sheet1.createRow( (short) 1 );
HSSFRow row2 = sheet1.createRow( (short) 2 );
HSSFCell cell = row.createCell( (short) 1 );
cell.setCellValue( "This is a test of merging" );
Region region = new Region( 1, (short) 1, 4, (short) 4 );
sheet1.addMergedRegion( region );
// Set the border and border colors.
final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
HSSFRegionUtil.setBorderBottom( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderTop( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderLeft( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderRight( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
// Shows some usages of HSSFCellUtil
HSSFCellStyle style = wb.createCellStyle();
style.setIndention((short)4);
HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
// Write out the workbook
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();
19.在工作单中移动行,调整行的上下位置
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.// Create various cells and rows for spreadsheet.
4.// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
5.sheet.shiftRows(5, 10, -5);
6.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
7.wb.write(fileOut);
8.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
// Create various cells and rows for spreadsheet.
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
sheet.shiftRows(5, 10, -5);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
20.选种指定的工作单
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.sheet.setSelected(true);
4.// Create various cells and rows for spreadsheet.
5.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
6.wb.write(fileOut);
7.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
sheet.setSelected(true);
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
21.工作单的放大缩小
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.sheet1.setZoom(3,4); // 75 percent magnification
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
22.头注和脚注
Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFHeader header = sheet.getHeader();
4.header.setCenter("Center Header");
5.header.setLeft("Left Header");
6.header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
7.HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFHeader header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft("Left Header");
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
23.图片的使用
Java代码
1.//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
2.ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
3.BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
4.ImageIO.write(bufferImg,"jpg",byteArrayOut);
5.
6.//读进一个excel模版
7.FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
8.fs = new POIFSFileSystem(fos);
9.//创建一个工作薄
10.HSSFWorkbook wb = new HSSFWorkbook(fs);
11.HSSFSheet sheet = wb.getSheetAt(0);
12.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
13.HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
14.patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);
//读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
fs = new POIFSFileSystem(fos);
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//-------------------------------以上实例代码均来自官方网站
//-------------------------------POI中使用的颜色是用颜色索引来实现,如下:
/*
* 颜色对照表 数字代表颜色索引
8: BLACK
60: BROWN
59: OLIVE_GREEN
58: DARK_GREEN
56: DARK_TEAL
18: DARK_BLUE
32: DARK_BLUE
62: INDIGO
63: GREY_80_PERCENT
53: ORANGE
19: DARK_YELLOW
17: GREEN
21: TEAL
38: TEAL
12: BLUE
39: BLUE
54: BLUE_GREY
23: GREY_50_PERCENT
10: RED
52: LIGHT_ORANGE
50: LIME
57: SEA_GREEN
49: AQUA
48: LIGHT_BLUE
20: VIOLET
36: VIOLET
55: GREY_40_PERCENT
14: PINK
33: PINK
51: GOLD
13: YELLOW
34: YELLOW
11: BRIGHT_GREEN
35: BRIGHT_GREEN
15: TURQUOISE
35: TURQUOISE
16: DARK_RED
37: DARK_RED
40: SKY_BLUE
61: PLUM
25: PLUM
22: GREY_25_PERCENT
45: ROSE
43: LIGHT_YELLOW
42: LIGHT_GREEN
41: LIGHT_TURQUOISE
27:LIGHT_TURQUOISE
44: PALE_BLUE
46: LAVENDER
9: WHITE
24: CORNFLOWER_BLUE
26: LEMON_CHIFFON
25: MAROON
28: ORCHID
29: CORAL
30: ROYAL_BLUE
31: LIGHT_CORNFLOWER_BLUE
*/
//----------------------------------------------------你可以按上面的方法来自定义颜色
/*
* 自定义颜色,去掉注释,贴加,其他则查看颜色对照表
HSSFPalette palette = this.getCustomPalette();
palette.setColorAtIndex(idx,
i, //RGB red (0-255)
j, //RGB green
k //RGB blue
);
*/
资源来自:http://tomgreenintel.iteye.com/blog/204106
- poi导入导出大数据量excel.rar (8.4 KB)
- 下载次数: 2
发表评论
-
JDK中的随机数机制探究
2017-05-31 21:25 1920今天有同事问起关于JDK1.8中默认是random问题 ... -
Netty5 AIO
2015-11-25 21:18 1659工程结构图: TimeServer.java文件内容如下: ... -
JDK1.7 AIO
2015-11-24 22:32 1029工程结构图: Helper.java文件内容如下: p ... -
java JAVA_OPTS javaagent
2015-11-23 15:48 1483附件里java JAVA_OPTS -javaagent的程序 ... -
dubbo源代码调试
2015-09-24 17:06 4177今天同事要调试zookeeper+dubbo源代码,所以做了以 ... -
any+ivy的使用
2014-11-29 16:25 2064javapro-用ivy编译版本.rar文件时示例代码。 i ... -
URLClassLoader初体验
2014-11-26 14:56 2158使用概要: File file = ... -
将输出流OutputStream转化为输入流InputStream的方法
2014-07-19 22:27 27638将输出流OutputStream转化为输入流InputSt ... -
JVM启动参数大全
2014-06-11 17:19 968http://www.blogjava.net/midstr/ ... -
Java实现aop案例
2014-04-27 11:33 1309MyPersonService.java代码如下: pac ... -
Java Socket长连接示例代码
2014-04-07 13:42 21728SocketListenerPusher.java代码如下: ... -
Java多线程文档
2013-10-06 10:56 1184附件里是Java多线程的pdf文档,写的比较好,可参考。 ... -
利用jackson包进行json字符串与pojo类之间的转换源代码
2012-10-05 19:35 1929附件里是利用jackson包进行json字符串与pojo类之间 ... -
Java Swing开发的文件内容加密
2012-09-02 14:52 1197主要是javax.swing.JFrame和java.awt. ... -
mysql和Oracle在对clob和blob字段的处理
2012-08-25 18:50 30855一、MySQL与Oracle数据库如何处理Clob,Bl ... -
用JAVA Bean 反射得到set,get方法
2011-10-17 19:39 9041/** * java反射bean的get方法 ... -
Oracle自带连接池应用
2011-08-31 13:27 2035依赖ojdbc14.jar包 OraclePooled.ja ... -
利用commons-digester3-30.jar解析xml文件
2011-08-28 14:05 3945此方式解析需要commons-beanutils-1.8.3. ... -
非阻塞的HTTP服务器
2011-08-27 21:23 1316附件里为非阻塞HTTP服务器的相关Java代码。 HttpS ... -
Java.nio
2011-08-27 21:17 1368附件里为阻塞模式、非阻塞模式、阻塞和非阻塞的混合模式代码。 ...
相关推荐
POI整理的完整详细的开发资料,详细讲解如何解析excel开发,
自己整理出来的POI文档,里面有大多数POI基本的使用方法
针对使用JAVA POI 4.0的Jar包进行了整理,提取了必要的jar整理打包LIb,并提供了 对Excel读写整理的可运行全工程。同类POI版本最高,最便宜的
poi-3.7.rar poi-apidocs.zip poi-scratchpad-3.7-sources.jar POI帮助文档.doc poi教程.rar POI整理.doc
今天用POI解析了excel,现在整理了一下,附件中包含POI操作2007office需要用到的所有jar包 dom4j-1.6.1.jar geronimo-stax-api_1.0_spec-1.0.jar ooxml-schemas-1.0.jar openxml4j-bin-beta.jar poi-3.0-FINAL.jar ...
常用的JAVA,Android包库,闲的整理一下打包上传,math3主要用于线性计算,回归分析等,jama主要用于矩阵计算,Poi用于输出大量数据到Excel,再导入matlab。commonIO也是用于输入输出流
poi-4.1.2需要的jar包,需要使用的朋友可以直接用,从maven库整理出来的。
最全JAVA实现Office在线预览调研,OpenOffice,LibreOffice,POI等已有技术归纳整理,附带链接。
POI_3.8_API.CHM; POI3.5_HSSF_和XSSF_Excel操作快速入门手册.pdf; poi-bin-3.9-20121203.zip; poi使用总结.txt; POI整理.doc;
全国大多数国家POI数据,自己整理了很久的资源 大家需要的自行下载。
poi解析excel所需所有jar包
精心收集而来,本人亲扯可用然后才整理出来的,最新的poi 配套的jar包。poi-3.12-beta1-20150228.jar,stax-api-1.0.1.jar,org.apache.poi.xwpf.converter.core-1.0.0.jar 。请把l里面Android的jar包忽略掉 那个是...
POI读写海量Excel(代码)。整理版~
java+poi+excel读取写入等操作所有jar(最新支持excel2013)Java实用源码整理learns
个人整理的POI解析EXCEL常用API详解 里面包括我在使用过程中的一些心得
东莞市公司企业poi数据2020年wsgs84坐标.xlsx东莞市公司企业poi数据2020年wsgs84坐标.xlsx