poi导出excel文件|Java 利用poi 导出excel表格如何在导出时自由选择路径

poi导出excel文件|Java 利用poi 导出excel表格如何在导出时自由选择路径的第1张示图

1. 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");

2. 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);

}

}

3. java用poi导出excel文件,打开导出的文件时报错,怎么办

两个原因:

1.你的excel模版本身有问题,可以尝试新建一个模版。

2.你的excel使用了一些POI不支持的函数。

解决办法:

另存是由excel重写了完整的文件,可以解决问题。

关闭文件例子:

FileOutputStream os = new FileOutputStream("workbook.xls");

wb.write(os);

os.close();

4. 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;}}

5. 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");

6. 怎么利用POI 从JSP表格中导出excel文件

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

7. springmvc使用poi导出excel需要什么jar

Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。这里的方法支持导出excel至项目所在服务器,或导出至客户端浏览器供用户下载,下面我把两个实例都放出来。1.下载所需POI的jar包,并导入项目。2.添加一个User类,用于存放用户实体,类中内容如下:1 package com.mvc.po; 2 3 public class User { 4 private int id; 5 private String name; 6 private String password; 7 private int age; 8 9 public User() {10 11 }12 13 public User(int id, String name, String password, int age) {14 this.id = id;15 this.name = name;16 this.password = password;17 this.age = age;18 }19 public int getId() {20 return id;21 }22 public void setId(int id) {23 this.id = id;24 }25 public String getName() {26 return name;27 }28 public void setName(String name) {29 this.name = name;30 }31 public String getPassword() {32 return password;33 }34 public void setPassword(String password) {35 this.password = password;36 }37 public int getAge() {38 return age;39 }40 public void setAge(int age) {41 this.age = age;42 }43 }3.添加一个UserController类,类中内容如下:1 package com.mvc.controller; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 import javax.servlet.ServletOutputStream; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.springframework.stereotype.Controller;10 import org.springframework.beans.factory.annotation.Autowired;11 import org.springframework.web.bind.annotation.RequestMapping;12 import org.springframework.web.bind.annotation.ResponseBody;13 14 import com.mvc.po.User;15 import com.mvc.service.UserService;16 17 @Controller18 public class UserController {19 20 @Autowired21 private UserService userService;22 23 @RequestMapping("/export.do")24 public @ResponseBody String export(HttpServletResponse response){ 25 response.setContentType("application/binary;charset=UTF-8");26 try{27 ServletOutputStream out=response.getOutputStream();28 String fileName=new String(("UserInfo "+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())).getBytes(),"UTF-8");29 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");30 String[] titles = { "用户编号", "用户姓名", "用户密码", "用户年龄" }; 31 userService.export(titles, out);32 return "success";33 } catch(Exception e){34 e.printStackTrace();35 return "导出信息失败";36 }37 }38 }4.添加一个接口类UserService和实现类UserServiceImpl,类中内容如下:1 package com.mvc.service;2 3 import javax.servlet.ServletOutputStream;4 import com.mvc.po.User;5 6 public interface UserService {7 public void export(String[] titles, ServletOutputStream out);8 }1 package com.mvc.service.impl; 2 3 import java.text.SimpleDateFormat; 4 import java.util.List; 5 6 import javax.servlet.ServletOutputStream; 7 8 import com.mvc..UserDAO; 9 import com.mvc.po.User;10 import com.mvc.service.UserService;11 12 import org.apache.poi.hssf.usermodel.HSSFCell;13 import org.apache.poi.hssf.usermodel.HSSFCellStyle;14 import org.apache.poi.hssf.usermodel.HSSFRow;15 import org.apache.poi.hssf.usermodel.HSSFSheet;16 import org.apache.poi.hssf.usermodel.HSSFWorkbook;17 import org.springframework.beans.factory.annotation.Autowired;18 import org.springframework.stereotype.Service;19 20 @Service21 public class UserServiceImpl implements UserService {22 23 @Autowired24 private UserDAO userDAO;25 26 @Override27 public void export(String[] titles, ServletOutputStream out) { 28 try{29 // 第一步,创建一个workbook,对应一个Excel文件30 HSSFWorkbook workbook = new HSSFWorkbook();31 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet32 HSSFSheet hssfSheet = workbook.createSheet("sheet1");33 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short34 HSSFRow hssfRow = hssfSheet.createRow(0);35 // 第四步,创建单元格,并设置值表头 设置表头居中36 HSSFCellStyle hssfCellStyle = workbook.createCellStyle();37 //居中样式38 hssfCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);39 40 HSSFCell hssfCell = null;41 for (int i = 0; i < titles.length; i++) {42 hssfCell = hssfRow.createCell(i);//列索引从0开始43 hssfCell.setCellValue(titles[i]);//列名144 hssfCell.setCellStyle(hssfCellStyle);//列居中显示 45 }46 47 // 第五步,写入实体数据 48 List<User> users = userDAO.query(); 49 50 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");51 if(users != null && !users.isEmpty()){52 for (int i = 0; i < users.size(); i++) {53 hssfRow = hssfSheet.createRow(i+1); 54 User user = users.get(i);55 56 // 第六步,创建单元格,并设置值57 int userid = 0;58 if(user.getId() != 0){59 userid = user.getId();60 }61 hssfRow.createCell(0).setCellValue(userid);62 String username = "";63 if(user.getName() != null){64 username = user.getName();65 }66 hssfRow.createCell(1).setCellValue(username);67 String password = "";68 if(user.getPassword() != null){69 password = user.getPassword();70 }71 hssfRow.createCell(2).setCellValue(password);72 int age = 0;73 if(user.getAge() != 0){74 age = user.getAge();75 }76 hssfRow.createCell(3).setCellValue(age);77 }78 }79 80 // 第七步,将文件输出到客户端浏览器81 try {82 workbook.write(out);83 out.flush();84 out.close();85 86 } catch (Exception e) {87 e.printStackTrace();88 }89 }catch(Exception e){90 e.printStackTrace();91 throw new Exception("导出信息失败!");92 } 93 }94 }5.添加一个接口类UserDAO和实现类UserDAOImpl,类中内容如下:1 package com.mvc.;2 3 import java.util.List;4 import com.mvc.po.User;5 6 public interface UserDAO {7 List<User> query(); 8 }1 package com.mvc..impl; 2 3 import java.util.List; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 7 import com.mvc..UserDAO; 8 import com.mvc.po.User; 9 10 import org.springframework.stereotype.Repository;11 import org.springframework.beans.factory.annotation.Autowired;12 import org.springframework.jdbc.core.JdbcTemplate;13 import org.springframework.jdbc.core.RowMapper;14 15 @Repository16 public class UserDAOImpl implements UserDAO {17 18 @Autowired19 private JdbcTemplate jdbcTemplate;20 21 public List<User> query() {22 return this.jdbcTemplate.query("select * from student",23 new RowMapper<User>() {24 public User mapRow(ResultSet rs, int arg1)25 throws SQLException {26 return new User(rs.getInt("sId"),27 rs.getString("sName"), rs.getString("sPwd"), rs28 .getInt("sAge"));29 }30 });31 }32 }这样就完成了excel导出至客户端浏览器,当然有时候也会用到导出excel至服务器上。只需要对本文步骤4中的第七步文件输出方式进行修改,如下:1 // 第七步,将文件存到指定位置2 try {3 FileOutputStream fileOutputStream = new FileOutputStream("C:/user.xls");//指定路径与名字和格式4 workbook.write(fileOutputStream);//讲数据写出去5 fileOutputStream.close();//关闭输出流6 } catch (Exception e) {7 e.printStackTrace();8 }然后去除controller类中的out参数设置就ok了。也可以看出其实两种方式只是最终保存方式不同,其他步骤是共通的。

8. 如何使用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 blocke.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();}}}

9. java poi导出excel要双击才显示换行

对于Java POI,其提供的API中,没有提供直接设置单元格“折行表示”的属性或者方法。我之前做这个地方的时候,是利用读取Excel的模板来实现的。在模板文件中,对单元格设置好“折行表示”。Java POI调用之后,先读取模板文件中已经设置好“折行表示”的单元格的style。然后在输出Excel的文件中,对需要有“折行表示”的单元格,将这个style赋给它。这样就在最终生成的Excel看到折行表示的效果了。

10. poi导出excel的时候怎么选择导出路径

主要代码如下:后台输出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();前端代码:window.open("getExcelList","_blank");

未经允许不得转载:山九号 » poi导出excel文件|Java 利用poi 导出excel表格如何在导出时自由选择路径

赞 (0)