Java利用POI导入导出Excel中的数据
首先谈一下今天发生的一件开心的事,本着一颗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的。
1: String fileaddress="E:/博客统计.xlsx"; //读取Excel地址
2: XSSFWorkbook wb=null;
3: File f = new File(fileaddress);// 读取excel文件
4: FileInputStream is = new FileInputStream(f);// 创建文件流
5: if(fileaddress.toLowerCase().endsWith("xlsx")){
6: wb = new XSSFWorkbook(is); //如果xlsx版本的就创建XSSFWorkbook对象
7: } else if(fileName.toLowerCase().endsWith("xls")){
8: wb = new HSSFWorkbook(is); //如果xls版本的就创建HSSFWorkbook对象
9: }
.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().
1: XSSFSheet sheet = wb.getSheetAt(1); // 第一个工作表
2: int lastrow = sheet.getLastRowNum() + 1;
3: if(rowindex_<lastrow){
4: XSSFRow row = sheet.getRow(rowindex_);// 获得行对象
5: row.getCell(0).setCellValue(bolgname2);
6: row.getCell(1).setCellValue(linktime2);
7: row.getCell(2).setCellValue(linktitle2);
8: row.getCell(3).setCellValue(linkresult2);
9: row.getCell(4).setCellValue(linkred2);
10: row.getCell(5).setCellValue(linkpinglun2);
11: row.getCell(6).setCellValue(tuijianString2);
12: row.getCell(7).setCellValue(fanduiString2);
13: }
14: else{
15: XSSFRow row = sheet.createRow(rowindex_);
16: row.createCell(0).setCellValue(bolgname2);
17: row.createCell(1).setCellValue(linktime2);
18: row.createCell(2).setCellValue(linktitle2);
19: row.createCell(3).setCellValue(linkresult2);
20: row.createCell(4).setCellValue(linkred2);
21: row.createCell(5).setCellValue(linkpinglun2);
22: row.createCell(6).setCellValue(tuijianString2);
23: row.createCell(7).setCellValue(fanduiString2);
24: }
3.关闭流
.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; }
1: FileOutputStream fileOut = new FileOutputStream(fileaddress);
2: wb.write(fileOut);
3: is.close();
4: fileOut.close();
.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利用POI导入导出Excel中的数据的更多相关文章
- 如何利用Navicat导入/导出mssql中的数据
sqlserver,在第一次使用该软件进行"连接"的时候,会提示安装"Microsoft Sqlsever Navicat Client.",这时直接点击&qu ...
- POI导入导出excel(附工具类)
关于POI导出excel的功能我在前面的文章已经写过了,POI导出excel的三种方式 , 导出表格数据到excel并下载(HSSFWorkbook版) ,本篇文章主要是将导入导出功能进一步地封装,在 ...
- java使用户EasyExcel导入导出excel
使用alibab的EasyExce完成导入导出excel 一.准备工作 1.导包 <!-- poi 相关--> <dependency> <groupId>org. ...
- thinkphp导入导出excel表单数据
在PHP项目经常要导入导出Excel表单. 先去下载PHPExcel类库文件,放到相应位置. 我在thinkphp框架中的位置为ThinkPHP/Library/Org/Util/ 导入 在页面上传e ...
- C#导入导出Excel表的数据
一:C#导入导出EXCEL文件的类 代码如下: 首先将Microsoft Excel 14.0 Object Library 引用导入 using System; using System.Data; ...
- Java POI导入导出Excel
1.异常java.lang.NoClassDefFoundError: org/apache/poi/UnsupportedFileFormatException 解决方法: 使用的poi的相关jar ...
- POI导入导出Excel(HSSF格式,User Model方式)
1.POI说明 Apache POI是Apache软件基金会的开源代码库, POI提供对Microsoft Office格式档案读和写的功能. POI支持的格式: HSSF - 提供读写Microso ...
- java使用poi生成导出Excel(新)
导出样式: java代码: import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStre ...
- SpringMvc 使用poi导入导出Excel
Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...
随机推荐
- 关于新中新二代身份证读卡器DKQ-A16D的一些问题
今天拿到了新中新DKQ-A16D,随机光盘里有以下文件: 我遇到的问题是,如果直接打开\二代征SDK开发包\DLL\测试程序\C#_2008\WindowsFormsApplication1\目录下的 ...
- WebLogic部署
1.抓取解压WAR包,放在相应目录下 2.登录部署,激活 http://jingyan.baidu.com/article/c74d6000650d470f6b595d72.html Linux环境中 ...
- HDU 4940 Destroy Transportation system(无源汇有上下界最大流)
看不懂题解以及别人说的集合最多只有一个点..... 然后试了下题解的方法http://blog.sina.com.cn/s/blog_6bddecdc0102uzka.html 首先是无源汇有上下界最 ...
- monitor system
#!/bin/bash # #Snapshot_Stats - produces a report for system stats # This report will mail to root. ...
- 【leetcode】Minimum Depth of Binary Tree (easy)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- IOS-Social.framework
1.使用前 需要导入Social.framework 框架 2.实例代码(新浪微博为例) - (IBAction)shejiaoBtn { // 判断服务器是否可用 if ([SL ...
- C#封装C++DLL
1.新建一个C#-Windows-类库(用于创建C#类库(.dll)的项目)类型的工程 2.对于普通C++函数 XXXX_API void cppFun(int i); 在cs代码中添加 [DllIm ...
- [Android Pro] Gradle Tips#2-语法
referece to : http://blog.csdn.net/lzyzsd/article/details/46935063 在第一篇博客中,我讲解了关于tasks和构建过程中task的不同阶 ...
- October 1st 2016 Week 40th Saturday
Autumn, the year's last, loveliest smile. 秋,四季流转中的最后一抹,也是最美的那一抹微笑. I love autumn because it is the h ...
- 多线程编程4 - NSOperationQueue
一.简介 一个NSOperation对象可以通过调用start方法来执行任务,默认是同步执行的.也可以将NSOperation添加到一个NSOperationQueue(操作队列)中去执行,而且是异步 ...