poi操作excel2007(读取、生成、编辑)
因为现在再写excel2003版的比较low,所以我在这就不介绍了,直接介绍2007,我所用的编程软件是IDEA
poi操作office总共有6个jar包,在pom.xml文件中配置如下,也可下载后直接引jar包(快捷键ctrl+shift+alt+s----->modules,添加jar包即可)目前poi的jar包最高版本为3.1.6
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-examples -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-examples</artifactId>
<version>3.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-excelant -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-excelant</artifactId>
<version>3.16</version>
</dependency>
接下来进入正题:
1、读取excel
//读取excel表格中的数据,path代表excel路径
public void readExecl(String path) {
try {
//读取的时候可以使用流,也可以直接使用文件名
XSSFWorkbook xwb = new XSSFWorkbook(path);
//循环工作表sheet
for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if (xSheet == null) {
continue;
}
//循环行row
for (int numRow = 0; numRow <= xSheet.getLastRowNum(); numRow++) {
XSSFRow xRow = xSheet.getRow(numRow);
if (xRow == null) {
continue;
}
//循环列cell
for (int numCell = 0; numCell <= xRow.getLastCellNum(); numCell++) {
XSSFCell xCell = xRow.getCell(numCell);
if (xCell == null) {
continue;
}
//输出值
System.out.println("excel表格中取出的数据" + getValue(xCell));
}
} } } catch (IOException e) {
e.printStackTrace();
}
} /**
* 取出每列的值
*
* @param xCell 列
* @return
*/
private String getValue(XSSFCell xCell) {
if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(xCell.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
return String.valueOf(xCell.getNumericCellValue());
} else {
return String.valueOf(xCell.getStringCellValue());
}
}
2、从数据库中取出数据表,导入并生成excel
数据库表名为Vt_User,其他注释都写得很清楚
public void createExcel() {
try {
String path = "D:/test.xlsx";
// 创建新的Excel 工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"用户表"的工作表,其语句为:
XSSFSheet sheet = workbook.createSheet("用户表");
// 在索引0的位置创建行(最顶端的行)
XSSFRow row = sheet.createRow((short) 0);
//在索引0的位置创建单元格(左上端)
XSSFCell cell = row.createCell((short) 0);
//创建单元格样式
CellStyle cellStyle = workbook.createCellStyle();
// 设置这些样式
cellStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell = row.createCell((short) 0);
cell.setCellValue("用户id");
cell.setCellStyle(cellStyle);
cell = row.createCell((short) 1);
cell.setCellValue("姓名");
cell.setCellStyle(cellStyle);
cell = row.createCell((short) 2);
cell.setCellValue("别名");
cell.setCellStyle(cellStyle);
cell = row.createCell((short) 3);
cell.setCellValue("密码");
cell.setCellStyle(cellStyle);
cell = row.createCell((short) 4);
cell.setCellValue("外来id");
cell.setCellStyle(cellStyle);
//查询数据库中所有的数据
VtUserMapper mapper = getMapper(VtUserMapper.class);
VtUserCriteria cri = new VtUserCriteria();
cri.createCriteria().andUserEnabledEqualTo(1);
List<VtUser> list = mapper.selectByExample(cri);
/*//第一个sheet第一行为标题
XSSFRow rowFirst = sheet.createRow(0);
rowFirst.setHeightInPoints(21.75f);*/
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
VtUser stu = (VtUser) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(stu.getUserId());
row.createCell((short) 1).setCellValue(stu.getUserName());
row.createCell((short) 2).setCellValue(stu.getUserNameZn());
row.createCell((short) 3).setCellValue(stu.getUserPassword());
row.createCell((short) 4).setCellValue(stu.getUserForeignId());
sheet.autoSizeColumn((short) 0); //调整第一列宽度(自适应),只识别数字、字母
sheet.autoSizeColumn((short) 1); //调整第二列宽度
//调整第三列宽度,有中文,先判断这一列的最长字符串
int length = stu.getUserNameZn().getBytes().length;
sheet.setColumnWidth((short)2,(short)(length*2*256));
sheet.autoSizeColumn((short) 3); //调整第四列宽度
sheet.autoSizeColumn((short) 4); //调整第五列宽度
/*Font font = workbook.createFont();
font.setFontHeightInPoints((short)18); //字体大小
sheet.setDefaultRowHeightInPoints(21.75f);
font.setFontName("楷体");
font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗体
font.setColor(HSSFColor.GREEN.index); //绿字- 字体颜色*/
}
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(path);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
//清空缓冲区数据
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成...");
} catch (Exception e) {
System.out.println("已运行 xlCreate() : " + e);
}
}
3、修改excel
//修改excel表格,path为excel修改前路径(D:\\test.xlsx)
public void writeExcel3(String path) {
try {
//传入的文件
FileInputStream fileInput = new FileInputStream(path);
//poi包下的类读取excel文件 // 创建一个webbook,对应一个Excel文件
XSSFWorkbook workbook = new XSSFWorkbook(fileInput);
//对应Excel文件中的sheet,0代表第一个
XSSFSheet sh = workbook.getSheetAt(0);
//修改excle表的第5行,从第三列开始的数据
for (int i = 2; i < 4; i++) {
//对第五行的数据修改
sh.getRow(4).getCell((short) i).setCellValue(100210 + i);
}
//将修改后的文件写出到D:\\excel目录下
FileOutputStream os = new FileOutputStream("D:\\修改后test.xlsx");
// FileOutputStream os = new FileOutputStream("D:\\test.xlsx");//此路径也可写修改前的路径,相当于在原来excel文档上修改
os.flush();
//将Excel写出
workbook.write(os);
//关闭流
fileInput.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
poi操作excel2007(读取、生成、编辑)的更多相关文章
- POI操作Excel2007实例二之“SXSSFWorkbook”处理海量数据
转自:http://blog.csdn.net/little_stars/article/details/8266262 前文讲述了 POI 读取的基本操作,但后期 经过试验,当写入数据量超过5万条以 ...
- Java POI 操作Excel(读取/写入)
pom.xml依赖: <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi< ...
- java poi 操作
Java POI 操作Excel(读取/写入) https://www.cnblogs.com/dzpykj/p/8417738.html Java操作Excel之Poi基本操作 https://my ...
- POI操作Excel详解,读取xls和xlsx格式的文件
package org.ian.webutil; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...
- Java使用poi从数据库读取数据生成Excel表格
想要使用POI操作以xsl结尾的Excel,首先要下载poi相关的jar包,用到的jar有: poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.9.j ...
- POI操作Excel
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
- java使用Apache POI操作excel文件
官方介绍 HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is ...
- 使用POI操作Excel时对事先写入模板的公式强制执行
场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...
- 全面了解POI操作Microsoft Office(Word、Excel、PowerPoint)
POI 与 Microsoft Office 1. POI 简介 POI 是 Apache 下的 Jakata 项目的一个子项目,主要用于提供 java 操作 Microsoft Office 办公套 ...
随机推荐
- mysql修改配置文件
在Apache, PHP, MySQL的体系架构中,MySQL对于性能的影响最大,也是关键的核心部分.对于Discuz!论坛程序也是如此,MySQL的设置是否合理优化,直接影响到论坛的速度和承载量!同 ...
- js sort根据数值大小自动排序(转)
var arrDemo = new Array(); arrDemo[0] = 10; arrDemo[1] = 50; arrDemo[2] = 51; arrDemo[3] = 100; arrD ...
- HDU 4365
把涂色的格子按对称旋转至左上角. 当未涂色时,若要符合要求,则必须要求每一圈矩形都是上下左右对称的.注意是一圈的小矩形.对于N*N的阵,若最外层一圈的小矩形要符合要求,则(假设N%2==0)可以涂色的 ...
- Hadoop使用Java进行文件修改删除操作
Hadoop使用Java进行文件修改删除操作 学习了:http://blog.csdn.net/menghuannvxia/article/details/44651061 学习了:http://bl ...
- [HTML5] Semantics for accessibility
For example, when we use checkbox, if we do like this: <div class="inline-control sign-up co ...
- xhprof安装&&使用
听同事说起过一个php性能分析扩展,叫xhprof,近期了解了下. XHProf 是一个轻量级的分层性能測量分析器. 在数据收集阶段.它跟踪调用次数与測量数据,展示程序动态调用的弧线图. 它在报告.后 ...
- Docker+ELK搭建
换了个运行环境,重新搭建一套公司本地内部的ELK,之前也搭过(可访问:https://yanganlin.com/31.html),最近做什么事情都想用Docker,这次也用Docker,还算顺利,没 ...
- 初涉springboot
1.首先,我们需要了解微服务是什么? 微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用 ...
- QT-自定义信号和槽
前言:信号和槽是一种松耦合机制,或者说是一种分布式机制,信号广播出去,槽会自定义订阅接收. 一.新建工程 二.新建部件 拖入button按钮.修改内容为“发送自定义信号” 三.自定义发送信号 3.1 ...
- Qt-窗口部件概念介绍
前言:包括基础窗口部件QWidget.对话框QDialog.QFrame类族 .按钮部件.行编辑器.数值设定框以及滑块部件. 一.基础窗口部件QWidget 窗口部件(Widget)是Qt中建立用户界 ...