java读写excel文件
近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情。
读取的文件主要分两类:xls文件、xlsx文件。xls文件的相关操作用的是jxl.jar包,只要将这个包导入即可。xlsx文件的相关操作是利用apache的poi包。
一、xls文件(一个jar包:jxl.jar)
1)创建
package jexcel; import java.io.*;
import jxl.*;
import jxl.write.*; /**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class CreateXLS { public static void main(String[] args) {
try {
//open file.
WritableWorkbook book = Workbook.createWorkbook(new File("d:/Test.xls")); //create Sheet named "Sheet_1". 0 means this is 1st page.
WritableSheet sheet = book.createSheet("Sheet_1", ); //define cell column and row in Label Constructor, and cell content write "test".
//cell is 1st-Column,1st-Row. value is "test".
Label label = new Label(, , "test");
//add defined cell above to sheet instance.
sheet.addCell(label); //create cell using add numeric. WARN:necessarily use integrated package-path, otherwise will be throws path-error.
//cell is 2nd-Column, 1st-Row. value is 789.123.
jxl.write.Number number = new jxl.write.Number(, , 789.123);
//add defined cell above to sheet instance.
sheet.addCell(number); //add defined all cell above to case.
book.write();
//close file case.
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2)读取
package jexcel;
import java.io.*;
import jxl.*; /**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class ReadXLS { public static void main(String[] args) {
try {
Workbook book = Workbook.getWorkbook(new File("e:/pos/rule.xls"));
//get a Sheet object.
Sheet sheet = book.getSheet();
//get 1st-Column,1st-Row content.
// Cell cell = sheet.getCell(1, 2); //先是列序号,然后是行序号,都是从0开始算起
Cell[] cell=sheet.getColumn();
for(int i=;i<cell.length;i++){
String result = cell[i].getContents();
System.out.println(result);
} book.close();
} catch (Exception e) {
e.printStackTrace();
} }
}
3)修改
package jexcel; import java.io.*;
import jxl.*;
import jxl.write.*; /**
* @author Ken
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class UpdateXLS { public static void main(String[] args) {
try {
//get file.
Workbook wb = Workbook.getWorkbook(new File("d:/Test.xls"));
//open a copy file(new file), then write content with same content with Test.xls.
WritableWorkbook book =
Workbook.createWorkbook(new File("d:/Test.xls"), wb);
//add a Sheet.
WritableSheet sheet = book.createSheet("Sheet_2", );
sheet.addCell(new Label(, , "test2"));
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
二、xlsx文件(五个jar包:poi-3.8-20120326.jar; poi-ooxml-3.8-20120326.jar; poi-ooxml-schemas-3.8-20120326.jar; xmlbeans-2.3.0.jar; dom4j-1.6.1.jar)
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.logging.Logger; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class xlsx { public static void main(String[] args) {
// TODO Auto-generated method stub
poiExcel("e:/pos/rules.xlsx");
} private static void poiExcel(String saveName){
try{
String realPath =saveName;
File fileDes = new File(realPath);
InputStream str = new FileInputStream(fileDes);
XSSFWorkbook xwb = new XSSFWorkbook(str); //利用poi读取excel文件流
XSSFSheet st = xwb.getSheetAt(); //读取sheet的第一个工作表
int rows=st.getLastRowNum();//总行数
int cols;//总列数
//log.info("========行========"+rows);
for(int i=;i<rows;i++){
XSSFRow row=st.getRow(i);//读取某一行数据
if(row!=null){
//获取行中所有列数据
cols=row.getLastCellNum();
//log.info("========行========"+rows+"=====列========"+cols); for(int j=;j<cols;j++){
XSSFCell cell=row.getCell(j);
if(cell==null){
System.out.print(" ");
}else{
//判断单元格的数据类型
switch (cell.getCellType()) {
case XSSFCell.CELL_TYPE_NUMERIC: // 数字
System.out.print(cell.getNumericCellValue() + " ");
break;
case XSSFCell.CELL_TYPE_STRING: // 字符串
System.out.print(cell.getStringCellValue() + " ");
break;
case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean
System.out.println(cell.getBooleanCellValue() + " ");
break;
case XSSFCell.CELL_TYPE_FORMULA: // 公式
System.out.print(cell.getCellFormula() + " ");
break;
case XSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println("");
break;
case XSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println("故障");
break;
default:
System.out.print("未知类型 ");
break;
}
}
} }
}
}catch(IOException e){
e.printStackTrace();
} } }
代码全是网上贴来的,大家一起分享。。
想把jar包也贴上来的,但是不知道是不支持还是我自己摸索不出来,反正最后是没贴出来。。。
java读写excel文件的更多相关文章
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- java读写excel文件( POI解析Excel)
package com.zhx.base.utils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi ...
- 利用java读写Excel文件
一.读取Excel文件内容 java 代码 public static String readExcel(File file){ StringBuffer sb = new StringBuffer( ...
- Java读写Excel文件,利用POI
直接看工具类代码吧, package com.example.demo.util; import com.example.demo.entity.ExcelDataVO; import org.apa ...
- 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...
- 《手把手教你》系列技巧篇(六十七)-java+ selenium自动化测试 - 读写excel文件 - 中篇(详细教程)
1.简介 前面介绍了POI可以操作excel,也简单的提到另一个操作excle的工具,本篇介绍一个其他的可以操作excel的工具,但是这个工具有一个前提,excel文件版本只能是97-2003版本,如 ...
- 《手把手教你》系列技巧篇(六十八)-java+ selenium自动化测试 - 读写excel文件 - 下篇(详细教程)
1.简介 今天继续操作Excle,小伙伴或者童鞋们是不是觉得宏哥会介绍第三种工具操作Excle,今天不介绍了,有两种就够用了,其实一种就够用了,今天主要是来介绍如何使用不同的数据类型读取Excel文件 ...
- java读取excel文件(.xls,xlsx,csv)
前提,maven工程通过poi读写excel文件,需要在pom.xml中配置依赖关系: 在<dependencies>中添加如下代码 <dependency> <grou ...
- Apache POI 读写 Excel 文件
目录 写入 Excel 文件 读取 Excel 文件 遍历 Excel 文件 需要的 maven 依赖 完整代码 写入 Excel 文件 // 写入 Excel 文件 // ============= ...
随机推荐
- C语言之流的重定向
写c的小程序断不了需要输入输出,手动输入可太麻烦了.下面介绍IO的重定向方式: .重定向标准输入输出和错误,直接在命令行使用符号< > > >> >>等,还可 ...
- poj1279 半平面交
题意:没看懂= = sol:在纸上随便画两下就可以看出,答案即按逆时针方向建立line,求它们的半平面交的面积. 模板题.注意输出答案时输出ans+eps,否则可能会出现结果为-0.00的情况. #i ...
- C++ essentials 之 static 关键字
extraction from The C++ Programming Language, 4th. edition, Bjarne Stroustrup If no initializer is s ...
- JavaWeb学习总结-07 Filter 学习和使用
一 Filter简介 Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态 ...
- FFT质数打表程序
#include<bits/stdc++.h> using namespace std; typedef long long ll; void sol(ll x){ int y=0; fo ...
- Linux开放1521端口允许网络连接Oracle Listene
症状:1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动. lsnrctl status 查看listener状态 lsnrctl s ...
- JavaScript学习笔记——DOM_对document对象的内容、属性、样式的操作
javascript-对文档对象的内容.属性.样式的操作 一.操作内容 1. innerHTML 用来设置或获取对象起始和结束标签内的内容(识别html标签) 2. innerText 用来设置或获取 ...
- wordpress后台404页面
就在刚刚,boss需要看公司网站后台,网站是用wordpress搭的,发现全是404,蛋疼,于是google,下面是解决办法: location / { if (-f $request_filenam ...
- Python + OpenCV2 系列:2 - 图片操作
这些相当于我的学习笔记,所以并没有很强的结构性和很全的介绍,请见谅. 1 读取.写入图像 下面是一个简短的载入图像.打印尺寸.转换格式及保存图像为.png的例子: # -*- coding: utf- ...
- Flash Decompiler
http://www.sothink.com/product/flash-decompiler-for-mac/ http://blog.sina.com.cn/s/blog_697935ad0100 ...