Java利用POI导入导出Excel中的数据
2015-07-24 21:55
746 查看
首先谈一下今天发生的一件开心的事,本着一颗android的心我被分配到了PB组,身在曹营心在汉啊!好吧,今天要记录和分享的是Java利用POI导入导出Excel中的数据。下面POI包的下载地址http://poi.apache.org/download.html,有兴趣的朋友也可以去看看其中的API。
下面分享一下在对POI进行基本操作时觉得需要注意的两点:
1.POI中针对xlsx/xls是需要create different Workbook instance HSSFWorkbook是针对xls XSSFWorkbook是针对xlsx的。
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2.在向Excel中导入数据时需要明白sheet中是否有row这个对象,如果有的话就getRow(),gerCell(),setCellValue(),没有的话就需要createRow(),createCell(),setCellValue().
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
最后今天对博客统计修复了一个bug,发现正则表达式在控制字符串格式时很给力但是水很深需要好好学习,就把他当成下一个学习目标,然后下一次解决问题时,脑子里迸出来的就是他。
下面分享一下在对POI进行基本操作时觉得需要注意的两点:
1.POI中针对xlsx/xls是需要create different Workbook instance HSSFWorkbook是针对xls XSSFWorkbook是针对xlsx的。
String fileaddress="E:/博客统计.xlsx"; //读取Excel地址
[code]XSSFWorkbook wb=null;
File f = new File(fileaddress);// 读取excel文件
FileInputStream is = new FileInputStream(f);// 创建文件流
if(fileaddress.toLowerCase().endsWith("xlsx")){
wb = new XSSFWorkbook(is); //如果xlsx版本的就创建XSSFWorkbook对象
} else if(fileName.toLowerCase().endsWith("xls")){
wb = new HSSFWorkbook(is); //如果xls版本的就创建HSSFWorkbook对象
}
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
2.在向Excel中导入数据时需要明白sheet中是否有row这个对象,如果有的话就getRow(),gerCell(),setCellValue(),没有的话就需要createRow(),createCell(),setCellValue().
XSSFSheet sheet = wb.getSheetAt(1); // 第一个工作表
[code] int lastrow = sheet.getLastRowNum() + 1;
if(rowindex_<lastrow){
XSSFRow row = sheet.getRow(rowindex_);// 获得行对象
row.getCell(0).setCellValue(bolgname2);
row.getCell(1).setCellValue(linktime2);
row.getCell(2).setCellValue(linktitle2);
row.getCell(3).setCellValue(linkresult2);
row.getCell(4).setCellValue(linkred2);
row.getCell(5).setCellValue(linkpinglun2);
row.getCell(6).setCellValue(tuijianString2);
row.getCell(7).setCellValue(fanduiString2);
}
else{
XSSFRow row = sheet.createRow(rowindex_);
row.createCell(0).setCellValue(bolgname2);
row.createCell(1).setCellValue(linktime2);
row.createCell(2).setCellValue(linktitle2);
row.createCell(3).setCellValue(linkresult2);
row.createCell(4).setCellValue(linkred2);
row.createCell(5).setCellValue(linkpinglun2);
row.createCell(6).setCellValue(tuijianString2);
row.createCell(7).setCellValue(fanduiString2);
}
3.关闭流
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
FileOutputStream fileOut = new FileOutputStream(fileaddress);
[code] wb.write(fileOut);
is.close();
fileOut.close();
[/code]
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
最后今天对博客统计修复了一个bug,发现正则表达式在控制字符串格式时很给力但是水很深需要好好学习,就把他当成下一个学习目标,然后下一次解决问题时,脑子里迸出来的就是他。
相关文章推荐
- JAVA应用程序打包成windows下的可执行程序的方法
- 疯狂java讲义中的习题,设计一个简单的计算器。
- org.springframework.web.util.NestedServletException:
- Java 注解
- Spring(九)让Spring自动扫描和管理Bean
- Java Type VS Hibernate Type
- hadoop开发:eclipse运行wordcount实例
- How to Compile Java DBus
- java 命令行参数输入法的应用
- JAVA中的String连接性能
- Java设计模式之单例模式
- 设计模式第一讲:单例模式(IBM开发者论坛已经发表的文章)
- Java学习篇------Java文本框
- netBeans调试时不进入java内部类(jdk源码)的方法
- java.util.Random.nextInt(int n)方法实例
- MyEclipse常见错误汇总,中英注释版(长期更新)
- JDK开发WebService
- struts ognl表达式底层执行原理
- Java常用知识(长期更新)
- java多线程学习笔记(1)——基本概念介绍