导航:首页 > 编程语言 > poijs导出excel

poijs导出excel

发布时间:2023-04-23 12:01:23

1. java 使用POI导出Excel文件,打开导出文件时报错

在保护状态下execl的格式有可能正在被使用,你这边修改,准确说是线程冲突,一般excel值会作为导出文件的模板,是不会编辑的。你可以在读的时候判断execl是否正在被使用。
下面的代码问题,你可以参考
package com.hwt.glmf.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

/**
* 导出Excel公共方法
* @version 1.0
*
* @author wangcp
*
*/
public class ExportExcel extends BaseAction {

//显示的导出表的标题
private String title;
//导出表的列名
private String[] rowName ;

private List<Object[]> dataList = new ArrayList<Object[]>();

HttpServletResponse response;

//构造方法,传入要导出的数据
public ExportExcel(String title,String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}

/*
* 导出数据
* */
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 创建工作簿对象
HSSFSheet sheet = workbook.createSheet(title); // 创建工作表

// 产生表格标题行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);

//sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//获取列头样式对象
HSSFCellStyle style = this.getStyle(workbook); //单元格样式对象

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定义所需列数
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置创建行(最顶端的行开始的第二行)

// 将列头设置到sheet的单元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //创建列头对应个数的单元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //设置列头单元格的值
cellRowName.setCellStyle(columnTopStyle); //设置列头单元格样式
}

//将查询出的数据设置到sheet对应的单元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍历每个对象
HSSFRow row = sheet.createRow(i+3);//创建所需的行数

for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //设置单元格的数据类型
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //设置单元格的值
}
}
cell.setCellStyle(style); //设置单元格样式
}
}
//让列宽随着导出的列长自动适应
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//当前行未被使用过
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

if(workbook !=null){
try
{
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}catch(Exception e){
e.printStackTrace();
}

}

/*
* 列头单元格样式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
font.setFontHeightInPoints((short)11);
//字体加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/*
* 列数据信息单元格样式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 设置字体
HSSFFont font = workbook.createFont();
//设置字体大小
//font.setFontHeightInPoints((short)10);
//字体加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//设置字体名字
font.setFontName("Courier New");
//设置样式;
HSSFCellStyle style = workbook.createCellStyle();
//设置底边框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//设置底边框颜色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//设置左边框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//设置左边框颜色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//设置右边框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//设置右边框颜色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//设置顶边框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置顶边框颜色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在样式用应用设置的字体;
style.setFont(font);
//设置自动换行;
style.setWrapText(false);
//设置水平对齐的样式为居中对齐;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//设置垂直对齐的样式为居中对齐;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}
}

2. poi如何通过模板导出excel

共分为六部完成根据模板导出excel操作:
第一步、设置excel模板路径(setSrcPath)
第二步、设置要生成excel文件路径(setDesPath)
第三步、设置模板中哪个Sheet列(setSheetName)
第四步、获取所读取excel模板的对象(getSheet)
第五步、设置数据(分为6种类型数据:setCellStrValue、setCellDateValue、 setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)
第六步、完成导出 (exportToNewFile)

3. poi导出的excel求汇总,

poi导出的excel求汇总的步骤:
1、寻找poi所需要的包,导入到项目中。值得注意的是,不要找poi很老的jar包,很多方法是无效且不好用。建议版本高点。我使用的是poi-3.7版本
2、建立一个导出方法,创建excel表、表的工作空间、单元格如图所示
3、单元格中存入值,及改变单元格样式
4、输出到具体的路径。

4. 怎么利用POI 从jsP表格中导出excel文件

java中导出Excel有两个组件可以使用,一个是jxl,一个是POI,我这里用的是POI。导出是可以在服务器上生成文件,然后下载,也可以利用输出流直接在网页
中弹出对话框提示用户保存或下载。生成文件的方式会导致服务器中存在着垃圾文件,实现方式不太优雅,所以这里我采用的是后面直接通过输出流的方式。

5. 如何使用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中的数据从数据库查询出来的,最后就生成了一个List<Computer>对象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();
}
List<Computer> computers = new ArrayList<Computer>();
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();
}
List<Computer> computers = new ArrayList<Computer>();
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(List<Computer> 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();
}
}
}

6. java导出数据到excel的几种方法的比较

Excel的两种导出入门方法(JAVA与JS)

最近在做一个小项目作为练手,其中使用到了导出到Excel表格,一开始做的是使用JAVA的POI导出的,但因为我的数据是爬虫爬出来的,数据暂时并不保存在数据库或后台,所以直接显示在HTML的table,需要下载时又要将数据传回后台然后生成Excel文件,最后再从服务器下载到本地,过程几度经过网络传输,感觉比较耗时与浪费性能,于是想着在HTML中的Table直接导到Excel中节约资源

JAVA导出EXCEL(.xls)

导出Excel用的插件是apache的poi.jar,maven地址如下

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version></dependency>

1. 简单应用

先来个简化无样式的Excel导出,由于我的数据存在JSON中,所以形参是JSONArray,朋友们根据自己的实际数据类型(Map,List,Set等)传入即可 ,代码如下

/**
* 创建excel并填入数据
* @author LiQuanhui
* @date 2017年11月24日 下午5:25:13
* @param head 数据头
* @param body 主体数据
* @return HSSFWorkbook
*/
public static HSSFWorkbook expExcel(JSONArray head, JSONArray body) { //创建一个excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook(); //创建一个sheet工作表
HSSFSheet sheet = workbook.createSheet("学生信息");
//创建第0行表头,再在这行里在创建单元格,并赋值
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null; for (int i = 0; i < head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.getString(i));//设置值
}
//将主体数据填入Excel中
for (int i = 0, isize = body.size(); i < isize; i++) {
row = sheet.createRow(i + 1);
JSONArray stuInfo = body.getJSONArray(i); for (int j = 0, jsize = stuInfo.size(); j < jsize; j++) {
cell = row.createCell(j);
cell.setCellValue(stuInfo.getString(j));//设置值
}
} return workbook;
}

创建好Excel对象并填好值后(就是得到workbook),就是将这个对象以文件流的形式输出到本地上去,代码如下

/**
* 文件输出
* @author LiQuanhui
* @date 2017年11月24日 下午5:26:23
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path) {
OutputStream os=null; try {
os = new FileOutputStream(new File(path));
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

至此Excel的导出其实已经做完了。

2. 添加样式后导出

但通常这并不能满足我们的需求,因为通常是需要设置Excel的一些样式的,如字体、居中等等,设置单元格样式主要用到这个类(HSSFCellStyle)

HSSFCellStyle cellStyle = workbook.createCellStyle();

现在说说HSSFCellStyle都能干些什么

HSSFCellStyle cellStyle = workbook.createCellStyle();//创建单元格样式对象1.设置字体
HSSFFont font = workbook.createFont(); //font.setFontHeight((short)12);//这个设置字体会很大
font.setFontHeightInPoints((short)12);//这才是我们平常在Excel设置字体的值
font.setFontName("黑体");//字体:宋体、华文行楷等等
cellStyle.setFont(font);//将该字体设置进去2.设置对齐方式
cellStyle.setAlignment(horizontalAlignment);//horizontalAlignment参考下面给出的参数
//以下是最常用的三种对齐分别是居中,居左,居右,其余的写代码的时候按提示工具查看即可
HorizontalAlignment.CENTER
HorizontalAlignment.LEFT
HorizontalAlignment.RIGHT3.设置边框
cellStyle.setBorderBottom(border); // 下边框
cellStyle.setBorderLeft(border);// 左边框
cellStyle.setBorderTop(border);// 上边框
cellStyle.setBorderRight(border);// 右边框
//border的常用参数如下
BorderStyle.NONE 无边框
BorderStyle.THIN 细边框
BorderStyle.MEDIUM 中等粗边框
BorderStyle.THICK 粗边框//其余的我也描述不清是什么形状,有兴趣的到时可以直接测试

在经过一系列的添加样式之后,最后就会给单元格设置样式

cell.setCellStyle(cellStyle);

3. 自动调整列宽

sheet.autoSizeColumn(i);//i为第几列,需要全文都单元格居中的话,需要遍历所有的列数

4. 完整的案例

public class ExcelUtils { /**
* 创建excel并填入数据
* @author LiQuanhui
* @date 2017年11月24日 下午5:25:13
* @param head 数据头
* @param body 主体数据
* @return HSSFWorkbook
*/
public static HSSFWorkbook expExcel(JSONArray head, JSONArray body) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("学生信息");

HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;

HSSFCellStyle cellStyle = workbook.createCellStyle();
setBorderStyle(cellStyle, BorderStyle.THIN);
cellStyle.setFont(setFontStyle(workbook, "黑体", (short) 14));
cellStyle.setAlignment(HorizontalAlignment.CENTER);
for (int i = 0; i < head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.getString(i));
cell.setCellStyle(cellStyle);
}

HSSFCellStyle cellStyle2 = workbook.createCellStyle();
setBorderStyle(cellStyle2, BorderStyle.THIN);
cellStyle2.setFont(setFontStyle(workbook, "宋体", (short) 12));
cellStyle2.setAlignment(HorizontalAlignment.CENTER); for (int i = 0, isize = body.size(); i < isize; i++) {
row = sheet.createRow(i + 1);
JSONArray stuInfo = body.getJSONArray(i); for (int j = 0, jsize = stuInfo.size(); j < jsize; j++) {
cell = row.createCell(j);
cell.setCellValue(stuInfo.getString(j));
cell.setCellStyle(cellStyle2);
}
} for (int i = 0, isize = head.size(); i < isize; i++) {
sheet.autoSizeColumn(i);
} return workbook;
} /**
* 文件输出
* @author LiQuanhui
* @date 2017年11月24日 下午5:26:23
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path) {
OutputStream os=null; try {
os = new FileOutputStream(new File(path));
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 设置字体样式
* @author LiQuanhui
* @date 2017年11月24日 下午3:27:03
* @param workbook 工作簿
* @param name 字体类型
* @param height 字体大小
* @return HSSFFont
*/
private static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name); return font;
} /**
* 设置单元格样式
* @author LiQuanhui
* @date 2017年11月24日 下午3:26:24
* @param workbook 工作簿
* @param border border样式
*/
private static void setBorderStyle(HSSFCellStyle cellStyle, BorderStyle border) {
cellStyle.setBorderBottom(border); // 下边框
cellStyle.setBorderLeft(border);// 左边框
cellStyle.setBorderTop(border);// 上边框
cellStyle.setBorderRight(border);// 右边框
}
}

POI的功能其实还是很强大的,这里只介绍了Excel的一丁点皮毛给入门的查看,如果想对Excel进行更多的设置可以查看下面的这篇文章,有着大量的使用说明。
空谷幽澜的POI使用详解

JS导出EXCEL(.xls)

java的Excel导出提供了强大的功能,但也对服务器造成了一定资源消耗,若能使用客户端的资源那真是太好了

1. 简单应用

JS的导出Excel非常简单,只需要引用Jquery和tableExport.js并设置一个属性即可

<script src="<%=basePath%>/static/js/tableExport.js" type="text/javascript"></script><script type="text/javascript">
function exportExcelWithJS(){ //获取要导出Excel的表格对象并设置tableExport方法,设置导出类型type为excel
$('#tableId').tableExport({ type:'excel'
});
}</script><button class="btn btn-primary" type="button" style="float: right;" onclick="exportExcelWithJS()">下载本表格</button>

JS的导出就完成了,是不是特别简单

2. 进阶应用

但上面仅仅是个简单的全表无样式的导出
这tableExport.js还有一些其他功能,忽略行,忽略列,设置样式等,属性如下

<script type="text/javascript">
function exportExcelWithJS(){ //获取要导出Excel的表格对象并设置tableExport方法,设置导出类型type为excel
$('#tableId').tableExport({ type:'excel',//导出为excel
fileName:'2017工资表',//文件名
worksheetName:'11月工资',//sheet表的名字
ignoreColumn:[0,1,2],//忽略的列,从0开始算
ignoreRow:[2,4,5],//忽略的行,从0开始算
excelstyles:['text-align']//使用样式,不用填值只写属性,值读取的是html中的
});
}</script>

7. Java 利用poi 导出excel表格如何在导出时自由选择路径

导出时自由选择路径的代码如下:

1、后台输出Excel文件代码:


OutputStream output = response.getOutputStream();


response.reset();


response.setHeader("Content-disposition", "attachment; filename=" + path);


response.setContentType("Content-Type:application/vnd.ms-excel ");


wb.write(output);


output.close();

2、前端代码:

window.open("getExcelList","_blank");

8. 关于java poi 导出Excel如何导出的问题

简单的办法是,先做一份excel,这只好每一列的格式。把这个excel放到项目中,每次导出都是用这个excel当作模板复制一份即可。

9. poi导出Excel问题

你的方法应该是没有问题,404错误只是你的跳转没响应,估计跳转页面路径错了。没有你的配置文件不好说

10. java poi导出excel

用spire.xls.jar也可以导出excel,代码更简单

  1. import com.spire.xls.ExcelVersion;

  2. import com.spire.xls.Workbook;

  3. import com.spire.xls.Worksheet;


  4. public class InsertArray {


  5. public static void main(String[] args) {


  6. //创建Workbook对象

  7. Workbook wb = new Workbook();

  8. //获取第一张工作表

  9. Worksheet sheet = wb.getWorksheets().get(0);

  10. //定义一维数据

  11. String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};

  12. //将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入

  13. sheet.insertArray(oneDimensionalArray, 1, 1, true);

  14. //定义二维数组

  15. String[][] twoDimensionalArray = new String[][]{

  16. {"姓名", "年龄", "性别", "学历"},

  17. {"小张", "25", "男", "本科"},

  18. {"小王", "24", "男", "本科"},

  19. {"小李", "26", "女", "本科"}

  20. };

  21. //从指定单元格开始写入二维数组到工作表

  22. sheet.insertArray(twoDimensionalArray, 1, 3);

  23. //保存文档

  24. wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

  25. }

  26. }

阅读全文

与poijs导出excel相关的资料

热点内容
win10磁盘自检文件丢失 浏览:475
win10扫描的文件在哪里 浏览:615
pdf文件公章歪了怎么处理 浏览:322
java下载文件的路径 浏览:551
现在有哪些热门的软件编程 浏览:453
asp什么文件迅雷下载 浏览:381
巫妖王之怒升级路线 浏览:348
wps如何发送文件 浏览:359
网站怎么加流量 浏览:457
圣魔之光石破解版本 浏览:110
湖北文件柜多少钱一套 浏览:103
artlantis渲染器教程 浏览:679
360系统文件可以清理吗 浏览:256
extjsform样式 浏览:513
电信猫怎么设置wifi密码 浏览:785
p190文件用什么打开 浏览:252
怎么修改ps签署文件 浏览:847
怎么找到编程猫作品文件 浏览:647
铁路局的网站是多少 浏览:194
微信双号 浏览:926

友情链接