JAVA读取、写入Excel表格(含03版)
引言
工作中可能会遇到对Excel读取和写入,如果我们自己手动写的话,会很麻烦,但是Apache中有poi工具类。poi工具类封装好了对于Excel读取和写入,我们需要用的时候,直接调用该方法就好了。
注:03和07的写法不一致。
区别如下
// HSSFWorkbook 2003的excel .xls,XSSFWorkbook导入2007的excel .xlsx
HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File(file)));
XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(new File(file))));
代码实现
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.alibaba.fastjson.JSONObject;
/**
*
* Title: excelTest
* Description: excel表格读取
* 注意:引用poi 架包版本要一致
* 如:
* poi-3.13.jar
* poi-ooxml-3.13.jar
* poi-ooxml-schemas-3.13.jar
* poi-scratchpad-3.13.jar
* 这些架包版本随意
* stax-api.jar
* xmlbeans.jar
* dom4j.jar
* Version:1.0.0
* @author pancm
*/
public class excelTest {
private static final String path="D:\\file\\test.xlsx";
private static final String path1="D:\\file\\test1.xlsx";
public static void main(String[] args) throws FileNotFoundException, IOException {
readExcel(path);
writeExcel(path1);
}
/**
* 读取Excel表格内容
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void readExcel(String str) throws FileNotFoundException, IOException{
File file=new File(str);
// HSSFWorkbook 2003的excel .xls,XSSFWorkbook导入2007的excel .xlsx
// HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File(file)));
XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(file));
Sheet sheet=workbook.getSheetAt(0);//读取第一个 sheet
List list= new ArrayList<>();
Row row=null;
int count=sheet.getPhysicalNumberOfRows();
//逐行处理 excel 数据
for (int i = 1; i <count; i++) {
JSONObject json=new JSONObject();
row=sheet.getRow(i);
Cell cell0 = row.getCell(0);
//设置取值为String
//整数数据要转,否则会变成浮点数
cell0.setCellType(Cell.CELL_TYPE_STRING);
Cell cell1 = row.getCell(1);
cell1.setCellType(Cell.CELL_TYPE_STRING);
json.put("Id",cell0.toString()); //编号
json.put("Name",cell1.toString()); //名称
list.add(json);
System.out.println("json:"+json);
}
workbook.close();
System.out.println("list:"+list);
}
/**
* 写入Excel表格内容
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings({ "resource", "rawtypes", "unchecked" })
private static void writeExcel(String str) throws FileNotFoundException, IOException{
File file=new File(str);
// HSSFWorkbook 2003的excel .xls,XSSFWorkbook导入2007的excel .xlsx
// HSSFWorkbook workbook=new HSSFWorkbook(new FileInputStream(new File(file)));
XSSFWorkbook workbook=new XSSFWorkbook(new FileInputStream(file));
List resultList =new ArrayList<>();
Sheet sheet1 = workbook.createSheet();//创建 sheet 对象
Row row = sheet1.createRow(0);//第一行,标题
row.createCell(0).setCellValue("A");
row.createCell(1).setCellValue("B");
row.createCell(2).setCellValue("C");
row.createCell(3).setCellValue("D");
row.createCell(4).setCellValue("E");
//拼接数据
for(int i=1;i<=10;i++){
JSONObject json1=new JSONObject();
json1.put("A", i);
json1.put("B", i*2);
json1.put("C", i*3);
json1.put("D", i*4);
json1.put("E", i*5);
resultList.add(json1);
}
System.out.println("resultList:"+resultList);
Row row1;
for (int i = 1, len = resultList.size(); i <=len; i++) {//循环创建数据行
//因为第一行已经设置了,所以从第二行开始
row1 = sheet1.createRow(i);
JSONObject json=(JSONObject) resultList.get(i-1);
row1.createCell(0).setCellValue(json.getString("A"));
row1.createCell(1).setCellValue(json.getString("B"));
row1.createCell(2).setCellValue(json.getString("C"));
row1.createCell(3).setCellValue(json.getString("D"));
row1.createCell(4).setCellValue(json.getString("E"));
}
FileOutputStream fos = new FileOutputStream(path1);
workbook.write(fos);//写文件
fos.close();
System.out.println("写入成功!");
}
}
示例图
读取Excel
新建一个Excel表格,设置表格内容。
关闭此Excel,运行代码,打印获取的数据。
写入Excel
创建一个新的Excel。
关闭此Excel,运行代码
再次打开此Excel
JAVA读取、写入Excel表格(含03版)的更多相关文章
- JXL读取写入excel表格数据
问题描述: 使用java的jxl包创建.写入excel表格数据 问题解决: (1)说明 (2)写入execel数据 注: 以上是写入数据需要调用的函数接口 注: 具体接口调用过程,如上所示 (3)读取 ...
- Java读取word中表格
因为要新建一个站,公司要把word表格的部分行列存到数据库中.之前用java操作过excel,本来打算用java从word表格中读取数据,再存到数据库中,结果因为权限不够,无法访问公司要写的那个数据库 ...
- java读取大容量excel之二(空格、空值问题)
最近在项目中发现,对于Excel2007(底层根本是xml) ,使用<java读取大容量excel之一>中的方式读取,若待读取的excel2007文件中某一列是空值,(注意,所谓的空值是什 ...
- python数据写入Excel表格
from openpyxl import Workbook def main(): sheet_name = "表名1" row_count = 6 # 行数 info_resul ...
- java后台读取/解析 excel表格
需求描述 前台需要上传excel表格,提交到后台,后台解析并返回给前台,展示在前台页面上! 前台部分代码与界面 <th style="padding: 7px 1px;width:15 ...
- Python读取Json字典写入Excel表格的方法
需求: 因需要将一json文件中大量的信息填入一固定格式的Excel表格,单纯的复制粘贴肯定也能完成,但是想偷懒一下,于是借助Python解决问题. 环境: Windows7 +Python2.7 + ...
- 《程序实现》从xml、txt文件里读取数据写入excel表格
直接上码 import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java ...
- POI读取/写入Excel文件
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- R 脚本读取汇总 Excel 表格数据
主要用到了 xlsx 和 rJava 包,打开 Excel 文件,读取各表格数据,再写入到汇总表. 下图为处理前的原始数据表格: 下图为处理后的数据: 代码实现 安装&加载包的函数实现.ins ...
随机推荐
- Lua中metatable和__index的联系
Lua中metatable和__index的联系 可以参考 http://blog.csdn.net/xenyinzen/article/details/3536708 来源 http://blog. ...
- Ubuntu下比较通用的makefile实例
本文转自http://blog.chinaunix.net/uid-20608849-id-360294.html 笔者在写程序的时候会遇到这样的烦恼:一个项目中可能会有很多个应用程序,而新建一个应 ...
- Again Stone Game
Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains ...
- Codeforces Round #380 (Div. 2)D. Sea Battle
D. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Formatting the event object
尽量将IE与DOM函数事件对象不同的性质或方法转成DOM标准 EventUtil.formatEvent = function (oEvent) { if (isIE && ...
- ⒂bootstrap组件 折叠 基础案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Android开发之漫漫长途 Ⅰ——Android系统的创世之初以及Activity的生命周期
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>中的相关知识,再次表示该书 ...
- heapster源码分析——kubelet的api调用分析
一.heapster简介 什么是Heapster? Heapster是容器集群监控和性能分析工具,天然的支持Kubernetes和CoreOS.Kubernetes有个出名的监控agent---cAd ...
- day2--通过setup设置网卡
配置IP地址,安装完成centos之后,需要配IP地址,流程如下: 1.首先开启虚拟机,如下: 2.登陆账号,root账户登陆,如下: 3.输入setup,按回车键进入,设置IP的方法有很多种,此处采 ...
- git(2)----Git的常用撤销技巧与解决冲突方法
git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash pop回复. git rese ...