本文目录一览:
- 1、用poi导出excel设置列宽的方法
- 2、java poi导出excel
- 3、POI导出Excel,复制行
- 4、如何使用POI对Excel表进行导入和导出
- 5、POI动态生成Excel
- 6、多表逐级关联报表,使用poi方法导出excel
用poi导出excel设置列宽的方法
很多朋友都想知道poi导出excel设置列宽的 方法 ,那么,该怎么做呢,下面让我为你带来poi导出excel设置列宽的简单方法。
poi导出excel设置列宽步骤:
接下来 说说 sheet.setColumnWidth((short) 0, (short) 250);
第一个参数表示要为第几列设置,第二个参数表示列的宽度,看看上面的代码按说第一行第一列的单元格形状应该是个正方形,因为宽和高都是250,但是打开导出后的 Excel 发现宽度没有高度大,是个长方形,查看该列的宽度仅为7个像素,看来行高和列宽的单位是不一样的,同样换一算sheet.setColumnWidth((short) 0, (short) (35.7));表示高度为一个像素,同样设置列宽的像素为sheet.setColumnWidth((short) 0, (short) (35.7*n));//n为列高的像素数。
关于poi导出excel的相关 文章 推荐:
1. java简历模板
2. java简历填写范文3篇
java poi导出excel
用spire.xls.jar也可以导出excel, 代码更简单
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class InsertArray {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//定义一维数据
String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};
//将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入
sheet.insertArray(oneDimensionalArray, 1, 1, true);
//定义二维数组
String[][] twoDimensionalArray = new String[][]{
{"姓名", "年龄", "性别", "学历"},
{"小张", "25", "男", "本科"},
{"小王", "24", "男", "本科"},
{"小李", "26", "女", "本科"}
};
//从指定单元格开始写入二维数组到工作表
sheet.insertArray(twoDimensionalArray, 1, 3);
//保存文档
wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);
}
}
POI导出Excel,复制行
在日常开发中,导出Excel应该是很常见了,最近有一个需求要动态填充模板内容,试了很多办法,最后采用复制行来搞定。
上图显示的可多填,都有可能出现多个保证人或者房产抵押信息,这时候就要根据内容动态修改模板,然后再赋值导出了。
该示例工程已完善并提交到gitee仓库,地址如下:
rowcopy
如何使用POI对Excel表进行导入和导出
导入POI的jar包
新建一个项目,在根目录在新建一个lib文件夹,将jar包复制粘贴到lib文件夹后,右键将其添加到项目的build path中,最后的结果如图所示:
2
编写java类,新建一个实体类,比如我们要导出数据库的有关电脑的信息,那么就建一个Computer实体类,代码如下:
package com.qiang.poi;
public class Computer {
private int id;
private String name;
private String description;
private double price;
private double credit;
public int getId() {
return id;
}
public Computer(int id, String name, String description, double price,
double credit) {
super();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.credit = credit;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
}
3
新建一个写入excel的方法,如write2excel,参数可以后面边写边决定(站在一个不熟悉POI的角度)
public static void write2Excel(){}
4
创建操作Excel的HSSFWorkbook对象
HSSFWorkbook excel= new HSSFWorkbook();
创建HSSFSheet对象
Excel中的一个sheet(工作表)对应着java中的一个HSSFSheet对象,利用HSSFWorkbook对象可以创建一个HSSFSheet对象
如:创建一个sheet名为computer的excel
HSSFSheet sheet = excel.createSheet("computer");
创建第一行标题信息的HSSFRow对象
我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个HSSFRow对象,我们通过HSSFSheet对象就可以创建HSSFRow对象
如:创建表格中的第一行(我们常用来做标题的行) HSSFRow firstRow = sheet.createRow(0); 注意下标从0开始
创建标题行中的HSSFCell数组
当然,excel中每一行是由若干个单元格,我们常称为cell,它对应着java中的HSSFCell对象
如:创建5个单元格 HSSFCell cells[] = new HSSFCell[5];
//假设我们一行有五列数据
创建标题数据,并通过HSSFCell对象的setCellValue()方法对每个单元格进行赋值
既然单元格都准备好了,那最后是不是该填充数据了呀。对的,没错。填充数据之前,得把数据准备好吧,
数据:String[] titles = new String[]{"id","name","description","price","credit"};
插入一句话: 在这个时代,能让机器做的,尽量不让人来做,记住这句话。
好的,继续。现在就通过for循环来填充第一行标题的数据
for (int i = 0; i 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
数据分析
第一行标题栏创建完毕后,就准备填充我们要写入的数据吧,在java中,面向对象给我们带来的好处在这里正好体现了,没错
把要填写的数据封装在对象中,即一行就是一个对象,n行就是一个对象列表嘛,好的,走起。
创建对象Computer,私有属性id,name,description,price,credit,以及各属性的setter和getter方法,如步骤二所示。
假设我们要写入excel中的数据从数据库查询出来的,最后就生成了一个ListComputer对象computers
数据写入
具体数据有了,又该让机器帮我们干活了,向excel中写入数据。
for (int i = 0; i computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
将数据真正的写入excel文件中
做到这里,数据都写好了,最后就是把HSSFWorkbook对象excel写入文件中了。
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据已经写入excel"); //温馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
ListComputer computers = new ArrayListComputer();
computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));
computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));
computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));
computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));
computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));
write2excel(computers, file);
}
工程目录及执行main方法后的test1.xls数据展示
源码分享,computer就不贴了
package com.qiang.poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
ListComputer computers = new ArrayListComputer();
computers.add(new Computer(1,"宏碁","笔记本电脑",3333,9.0));
computers.add(new Computer(2,"苹果","笔记本电脑,一体机",8888,9.6));
computers.add(new Computer(3,"联想","笔记本电脑,台式机",4444,9.3));
computers.add(new Computer(4, "华硕", "笔记本电脑,平板电脑",3555,8.6));
computers.add(new Computer(5, "注解", "以上价格均为捏造,如有雷同,纯属巧合", 1.0, 9.9));
write2excel(computers, file);
}
public static void write2excel(ListComputer computers,File file) {
HSSFWorkbook excel = new HSSFWorkbook();
HSSFSheet sheet = excel.createSheet("computer");
HSSFRow firstRow = sheet.createRow(0);
HSSFCell cells[] = new HSSFCell[5];
String[] titles = new String[] { "id", "name", "description", "price",
"credit" };
for (int i = 0; i 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
for (int i = 0; i computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
POI动态生成Excel
项目功能里要求能够将展示的报表导出excel,因为报表的数据都是动态从list传进来的,所以使用了POI技术来动态构建excel文件。
百科里说POI是介个样子的
“ApachePOI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对MicrosoftOffice格式档案读和写的功能”
简单来说就是通过它的API可以进行创建/读取文档,sheet,行列单元格等操作,也可以设置文档的各个样式。
刚接触这个任务的时候查了很多资料,最后主要是参考了这篇文章,程序复制粘贴就跑得通,对POI的整个理解可以得到很好地提升。
详解JAVA POI导出EXCEL报表的操作(包括各种格式及样式的实现)
然后参考着就实现了项目里要求的样子啦
=======================================================
百科中的示例附上作为下次使用的备忘。
创建Excel 文档
示例1将演示如何利用Jakarta POI API 创建Excel 文档。
示例1程序如下:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java .io.FileOutputStream;
public class CreateXL {
/** Excel 文件要存放的位置,假定在D盘下*/
public static String outputFile="D:/test.xls";
public static void main(String argv[]){
try{
// 创建新的Excel 工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 在Excel工作簿中建一工作表,其名为缺省值
// 如要新建一名为"效益指标"的工作表,其语句为:
// HSSFSheet sheet = workbook.createSheet("效益指标");
HSSFSheet sheet = workbook.createSheet();
// 在索引0的位置创建行(最顶端的行)
HSSFRow row = sheet.createRow((short)0);
//在索引0的位置创建单元格(左上端)
HSSFCell cell = row.createCell((short) 0);
// 定义单元格为字符串类型
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
// 在单元格中输入一些内容
cell.setCellValue("增加值");
// 新建一输出文件流
FileOutputStream fOut = new FileOutputStream(outputFile);
// 把相应的Excel 工作簿存盘
workbook.write(fOut);
fOut.flush();
// 操作结束,关闭文件
fOut.close();
System.out.println("文件生成...");
}catch(Exception e) {
System.out.println("已运行 xlCreate() : " + e );
}
}
}
读取Excel文档中的数据
示例2将演示如何读取Excel文档中的数据。假定在D盘JTest目录下有一个文件名为test1.xls的Excel文件。
示例2程序如下:
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java .io.FileInputStream;
public class ReadXL {
/** Excel文件的存放位置。注意是正斜线*/
public static String fileToBeRead="D:/test1.xls";
public static void main(String argv[]){
try{
// 创建对Excel工作簿文件的引用
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
// 创建对工作表的引用。
// 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")
HSSFSheet sheet = workbook.getSheet("Sheet1");
// 也可用getSheetAt(int index)按索引引用,
// 在Excel文档中,第一张工作表的缺省索引是0,
// 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
// 读取左上端单元
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
// 输出单元内容,cell.getStringCellValue()就是取所在单元的值
System.out.println("左上端单元是: " + cell.getStringCellValue());
}catch(Exception e) {
System.out.println("已运行xlRead() : " + e );
}
}
}
设置单元格格式
在这里,我们将只介绍一些和格式设置有关的语句,我们假定workbook就是对一个工作簿的引用。在Java中,第一步要做的就是创建和设置 字体和单元格的格式,然后再应用这些格式:
1、创建字体,设置其为红色、粗体:
HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
2、创建格式
HSSFCellStyle cellStyle= workbook.createCellStyle();
cellStyle.setFont(font);
3、应用格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("标题 ");
处理WORD文档
import java .io.*;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
public class TestPoi {
public TestPoi() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("D:/a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
//System.out.println("the result length is"+str.length());
System.out.println(str);
}
}
搜集链接 方便以后查阅
POI操作Excel常用方法总结
自己封装的poi操作excel工具类
多表逐级关联报表,使用poi方法导出excel
多表关联导出,简单介绍:有A1表,A2表,B表,C表,A1和A2是一对多的关系,B表和A1表也是一对多的关系,C表和B表是一对多的关系。这是简化的业务逻辑,表之间的关系就类似这种。完成对关联导出。
熟悉poi导出的可能会知道,报表的导出总体思路是将数据按照excel的每一行去写入数据的,假如是有序的数据还好说点,比如单表的导出,直接单表查询出来,循环写入每一个excel行就行,这种做法很简单。但是假如遇到一对多的表关联数据呢?更复杂一点的就是一对多对多,每一行都要根据数据来变化结构。
这种复杂逻辑我真的没有什么特别好的方法来解决,只能将这些逻辑拆分,将所有数据都查询出来,然后按照每一个一个多关系拆分,利用stream表达式进行快速筛选,完成数据的重组,写入excel中。