导航:首页 > 编程语言 > java判断excel导出成功poi

java判断excel导出成功poi

发布时间:2021-12-03 00:26:52

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

两个原因:

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

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

解决办法:

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

关闭文件例子:

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

wb.write(os);

os.close();

Ⅱ Java POI怎样判断导出的Excel数据是否正确地保存到本地。考虑网络中断,客户不正确操作等因素。

无法判断,POI只提供的API,只有对MS的office的读写功能。保存的excel会先生成一个临时文件,再传到client端。传输的问题本身不是POI来负责的。至于Http的文件传输,你就得用其他方法判断了。

Ⅲ 如何用JAVA导出Excel(使用POI)

response.setContentType("bin");
response.setHeader("Content-disposition","attachment;filename=test.xls");
你在servlet里面设置响应头为这样,然后就能够实现下载了,然后使用输出流进行输出下载
.....还有问题的话就问..
你要是想全部代码都写的话..加我扣扣 1195391953..

Ⅳ java从数据库中导出excel poi

这个我做奖金,考勤系统的时候经常用到,是一个方法,希望能帮到你。
用的 poi:
/**
* 将奖金列表转换为奖金报表
* @param bonus
* @return byte[]
*/
private byte[] mainProcessBonusListToReport(List<Bonus> bonuses){
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet hssfSheet =hssfWorkbook.createSheet("总奖金报表单");
/*第一行单元格合并*/

hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 12));
/*第一行*/

HSSFRow hssfRow = hssfSheet.createRow(0);
HSSFCell hssfCell=hssfRow.createCell(0);
hssfCell.setCellValue("派单业务考核记录");
/*第二行*/

hssfRow = hssfSheet.createRow(1);
/*列名*/

String[] titles = {"序号","工号","话务员姓名","规范奖考核","业务奖金","话务奖金","服务质量奖金","星级系数","组长津贴","总奖金","增资奖金","英语翻译","实发总奖金"};
/*for循环生成列名*/

for (int i = 0; i < titles.length; i++) {
hssfCell = hssfRow.createCell(i);
hssfCell.setCellValue(titles[i]);
}
/*填充数据*/

int rowIndex=2;
for (Bonus bonus : bonuses) {
hssfRow = hssfSheet.createRow(rowIndex);
hssfCell = hssfRow.createCell(0);
hssfCell.setCellValue(rowIndex-1);
hssfCell = hssfRow.createCell(1);
hssfCell.setCellValue(bonus.getWorkNumber());
hssfCell = hssfRow.createCell(2);
hssfCell.setCellValue(bonus.getName());
hssfCell = hssfRow.createCell(3);
hssfCell.setCellValue(bonus.getStandardPerformance());
hssfCell = hssfRow.createCell(4);
hssfCell.setCellValue(bonus.getBusinessBonus());
hssfCell = hssfRow.createCell(5);
hssfCell.setCellValue(bonus.getCallBonus());
hssfCell = hssfRow.createCell(6);
hssfCell.setCellValue(bonus.getServiceQualityBonus());
hssfCell = hssfRow.createCell(7);
hssfCell.setCellValue(bonus.getStarCoefficient());
hssfCell = hssfRow.createCell(8);
hssfCell.setCellValue(bonus.getGroupLeaderAllowance());
hssfCell = hssfRow.createCell(9);
hssfCell.setCellValue(bonus.getTotalBonus());
hssfCell = hssfRow.createCell(10);
hssfCell.setCellValue(bonus.getAdditionalBonus());
hssfCell = hssfRow.createCell(11);
hssfCell.setCellValue(bonus.getEnglishTranslateBonus());
hssfCell = hssfRow.createCell(12);
hssfCell.setCellValue(bonus.getActualTotalBonus());
rowIndex++;
}
byte[] bytes = TypeUtils.HSSFWorkbookToByteArray(hssfWorkbook);
return bytes;
}

Ⅳ java如何导出excel表格,如果用poi,java代码如何实现.,求代码!!!

项目结构:

xls:

\\\

XlsMain .java 类
//该类有main方法,主要负责运行程序,同时该类中也包含了用poi读取Excel(2003版)

*

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
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;

/**
*
* @author Hongten</br>
*
* 参考地址:http://hao0610.iteye.com/blog/1160678
*
*/
public class XlsMain {

public static void main(String[] args) throws IOException {
XlsMain xlsMain = new XlsMain();
XlsDto xls = null;
List<XlsDto> list = xlsMain.readXls();

try {
XlsDto2Excel.xlsDto2Excel(list);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < list.size(); i++) {
xls = (XlsDto) list.get(i);
System.out.println(xls.getXh() + " " + xls.getXm() + " "
+ xls.getYxsmc() + " " + xls.getKcm() + " "
+ xls.getCj());
}

}

/**
* 读取xls文件内容
*
* @return List<XlsDto>对象
* @throws IOException
* 输入/输出(i/o)异常
*/
private List<XlsDto> readXls() throws IOException {
InputStream is = new FileInputStream("pldrxkxxmb.xls");
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
XlsDto xlsDto = null;
List<XlsDto> list = new ArrayList<XlsDto>();
// 循环工作表Sheet
for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null) {
continue;
}
// 循环行Row
for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
HSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null) {
continue;
}
xlsDto = new XlsDto();
// 循环列Cell
// 0学号 1姓名 2学院 3课程名 4 成绩
// for (int cellNum = 0; cellNum <=4; cellNum++) {
HSSFCell xh = hssfRow.getCell(0);
if (xh == null) {
continue;
}
xlsDto.setXh(getValue(xh));
HSSFCell xm = hssfRow.getCell(1);
if (xm == null) {
continue;
}
xlsDto.setXm(getValue(xm));
HSSFCell yxsmc = hssfRow.getCell(2);
if (yxsmc == null) {
continue;
}
xlsDto.setYxsmc(getValue(yxsmc));
HSSFCell kcm = hssfRow.getCell(3);
if (kcm == null) {
continue;
}
xlsDto.setKcm(getValue(kcm));
HSSFCell cj = hssfRow.getCell(4);
if (cj == null) {
continue;
}
xlsDto.setCj(Float.parseFloat(getValue(cj)));
list.add(xlsDto);
}
}
return list;
}

/**
* 得到Excel表中的值
*
* @param hssfCell
* Excel中的每一个格子
* @return Excel中每一个格子中的值
*/
@SuppressWarnings("static-access")
private String getValue(HSSFCell hssfCell) {
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
// 返回布尔类型的值
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
// 返回数值类型的值
return String.valueOf(hssfCell.getNumericCellValue());
} else {
// 返回字符串类型的值
return String.valueOf(hssfCell.getStringCellValue());
}
}

}

XlsDto2Excel.java类
//该类主要负责向Excel(2003版)中插入数据

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
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;

public class XlsDto2Excel {

/**
*
* @param xls
* XlsDto实体类的一个对象
* @throws Exception
* 在导入Excel的过程中抛出异常
*/
public static void xlsDto2Excel(List<XlsDto> xls) throws Exception {
// 获取总列数
int CountColumnNum = xls.size();
// 创建Excel文档
HSSFWorkbook hwb = new HSSFWorkbook();
XlsDto xlsDto = null;
// sheet 对应一个工作页
HSSFSheet sheet = hwb.createSheet("pldrxkxxmb");
HSSFRow firstrow = sheet.createRow(0); // 下标为0的行开始
HSSFCell[] firstcell = new HSSFCell[CountColumnNum];
String[] names = new String[CountColumnNum];
names[0] = "学号";
names[1] = "姓名";
names[2] = "学院";
names[3] = "课程名";
names[4] = "成绩";
for (int j = 0; j < CountColumnNum; j++) {
firstcell[j] = firstrow.createCell(j);
firstcell[j].setCellValue(new HSSFRichTextString(names[j]));
}
for (int i = 0; i < xls.size(); i++) {
// 创建一行
HSSFRow row = sheet.createRow(i + 1);
// 得到要插入的每一条记录
xlsDto = xls.get(i);
for (int colu = 0; colu <= 4; colu++) {
// 在一行内循环
HSSFCell xh = row.createCell(0);
xh.setCellValue(xlsDto.getXh());
HSSFCell xm = row.createCell(1);
xm.setCellValue(xlsDto.getXm());
HSSFCell yxsmc = row.createCell(2);
yxsmc.setCellValue(xlsDto.getYxsmc());
HSSFCell kcm = row.createCell(3);
kcm.setCellValue(xlsDto.getKcm());
HSSFCell cj = row.createCell(4);
cj.setCellValue(xlsDto.getCj());
(xlsDto.getMessage());
}
}
// 创建文件输出流,准备输出电子表格
OutputStream out = new FileOutputStream("POI2Excel/pldrxkxxmb.xls");
hwb.write(out);
out.close();
System.out.println("数据库导出成功");
}

}

XlsDto .java类
//该类是一个实体类

public class XlsDto {
/**
* 选课号
*/
private Integer xkh;
/**
* 学号
*/
private String xh;
/**
* 姓名
*/
private String xm;
/**
* 学院
*/
private String yxsmc;
/**
* 课程号
*/
private Integer kch;
/**
* 课程名
*/
private String kcm;
/**
* 成绩
*/
private float cj;
public Integer getXkh() {
return xkh;
}
public void setXkh(Integer xkh) {
this.xkh = xkh;
}
public String getXh() {
return xh;
}
public void setXh(String xh) {
this.xh = xh;
}
public String getXm() {
return xm;
}
public void setXm(String xm) {
this.xm = xm;
}
public String getYxsmc() {
return yxsmc;
}
public void setYxsmc(String yxsmc) {
this.yxsmc = yxsmc;
}
public Integer getKch() {
return kch;
}
public void setKch(Integer kch) {
this.kch = kch;
}
public String getKcm() {
return kcm;
}
public void setKcm(String kcm) {
this.kcm = kcm;
}
public float getCj() {
return cj;
}
public void setCj(float cj) {
this.cj = cj;
}

}

后台输出:
数据库导出成功
1.0 hongten 信息技术学院 计算机网络应用基础 80.0
2.0 王五 信息技术学院 计算机网络应用基础 81.0
3.0 李胜基 信息技术学院 计算机网络应用基础 82.0
4.0 五班古 信息技术学院 计算机网络应用基础 83.0
5.0 蔡诗芸 信息技术学院 计算机网络应用基础 84.0

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

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

Ⅶ java web poi如何按查询结果导出相应的Excel最好有相应代码实例。

package com.aerolink.aocs.util.fileUtil;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

//import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class WriteExcelNew {

/**
* Excel文件
*/
private XSSFWorkbook wb = null;

/**
* 输出Excel文件中的表对象
*/
private XSSFSheet sheet = null;

/**
* 输出文件流
*/
private FileOutputStream fileOut = null;

/**
* 输出文件名用户自定义
*/
private String outputFilename = null;

/**
* 单元格样式
*/
private XSSFCellStyle cellStyle = null;

// private String newsheet = null; //输出Excel文件中的表名用户自定义
/**
* 行
*/
private XSSFRow row=null;

/**
*
*/
private int rowNumber=-1;
/**
* @param outputFilename
* @param newsheet
*/
public WriteExcelNew(String outputFilename, String newsheet) {

wb = new XSSFWorkbook();
//wb.setSheetName(1, "qwe");//设置第一张表的名称
sheet = wb.createSheet(newsheet);
//sheet.setColumnWidth(1, 40);//第一行 列宽
this.outputFilename = outputFilename;
// this.newsheet = newsheet;
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,int value)方法:
* </p>
* <p>
* 将int数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, int value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
//row.setHeight((short)50);//行高
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,String value)方法:
* </p>
* <p>
* 将String数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, String value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,double value)方法:
* </p>
* <p>
* 将double数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, double value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,boolean
* value)方法:
* </p>
* <p>
* 将boolean数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, boolean value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,Date value)方法:
* </p>
* <p>
* 将Date数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, Date value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}

/**
* <p>
* Description:exportToExcelFile(short rownum,short cellnum,Calendar
* value)方法:
* </p>
* <p>
* 将Calendar数据写入Execl文件的表中
* </p>
*
* @param rownum
* @param cellnum
* @param value
*/
public void exportToExcelFile(int rownum, int cellnum, Calendar value) {
if(rowNumber==-1){ //在poi3.8版本中,不这样做,只能写入最后一个单元格
rowNumber=rownum;
row = sheet.createRow(rownum);
}else if(rowNumber!=rownum){
rowNumber=rownum;
row = sheet.createRow(rownum);
}
XSSFCell cell = row.createCell(cellnum);
cell.setCellValue(value);
if(cellStyle==null){
setCellStyle("center","center","",false);
}
cell.setCellStyle(cellStyle);
}
/**
* 合并单元格 2012-3-30 孙贵春 add
* @param startRow
* @param startCol
* @param endRow
* @param endCol
*/
public void setMergedRegion(int startRow,int startCol,int endRow,int endCol){
sheet.addMergedRegion(new CellRangeAddress(startRow,endRow,startCol,endCol));//这与1.5版本明显不同
}
/**
* 设置单元格样式
* @param a_pos
* @param v_pos
* @param border
* @param wrap
*/
public void setCellStyle(String a_pos,String v_pos,String border,Boolean wrap){
cellStyle=wb.createCellStyle();
//水平位置
if(a_pos.equals("center")){
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
}else if(a_pos.equals("left")){
cellStyle.setAlignment(CellStyle.ALIGN_LEFT);
}else{
cellStyle.setAlignment(CellStyle.ALIGN_RIGHT);
}
//垂直位置
if(v_pos.equals("center")){
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
}else if(v_pos.equals("top")){
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_TOP);
}else{
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_BOTTOM);
}
//边框
if(border.equals("thin")){
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
}else if(border.equals("double")){
cellStyle.setBorderBottom(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderLeft(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderRight(CellStyle.BORDER_DOUBLE);
cellStyle.setBorderTop(CellStyle.BORDER_DOUBLE);
}else if(border.equals("thick")){
cellStyle.setBorderBottom(CellStyle.BORDER_THICK);
cellStyle.setBorderLeft(CellStyle.BORDER_THICK);
cellStyle.setBorderRight(CellStyle.BORDER_THICK);
cellStyle.setBorderTop(CellStyle.BORDER_THICK);
}else if(border.equals("medium")){
cellStyle.setBorderBottom(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderRight(CellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM);
}else if(border.equals("none")){
cellStyle.setBorderBottom(CellStyle.BORDER_NONE);
cellStyle.setBorderLeft(CellStyle.BORDER_NONE);
cellStyle.setBorderRight(CellStyle.BORDER_NONE);
cellStyle.setBorderTop(CellStyle.BORDER_NONE);
}else if(border.equals("dotted")){
cellStyle.setBorderBottom(CellStyle.BORDER_DOTTED);
cellStyle.setBorderLeft(CellStyle.BORDER_DOTTED);
cellStyle.setBorderRight(CellStyle.BORDER_DOTTED);
cellStyle.setBorderTop(CellStyle.BORDER_DOTTED);
}
//设置自动换行
cellStyle.setWrapText(wrap);
/*
cellStyle.setRotation((short)90);//设置单元格内文字旋转角度
XSSFFont font=wb.createFont();
font.setFamily(1);
font.setBold(true);
cellStyle.setFont(font);
*/
}

/**
* <p>
* Description: closeFileOut()方法:关闭文件输出流
* </p>
*
* @throws IOException
*/
public void closeFileOut() throws IOException {

fileOut = new FileOutputStream(outputFilename);
wb.write(fileOut);
fileOut.close();
}

/**
* <p>
* 测试方法
* </p>
*
* @param arg
*/
public static void main(String arg[]) {

try {
WriteExcelNew writeExcel = new WriteExcelNew("bak\\new.xls", "newsheet");
writeExcel.exportToExcelFile((short) 0, (short) 0, 99.99);
writeExcel.closeFileOut();
} catch (Exception e) {
System.out.println("Something Happen");
e.printStackTrace();
}
}
}

Ⅷ java如何,判断POI 导出Excel 是否成功

看是不是按你预期的逻辑执行结束的。

Ⅸ JAVA,POI导出EXCEL表,表中所有数据都是从后台直接获取,求指导,越详细越好

前几天刚好因抄为工作需要做过相应的excel处理程序编写,这里有一个例子,注释也很详细,具体参见doexeclsrcutilExeclUtil.java中的writeExcel方法,其中的写出文件的保存路径采用的是配置文件的形式,你也可以直接写死,具体的内容你就看代码吧

阅读全文

与java判断excel导出成功poi相关的资料

热点内容
iphone4充电图标 浏览:841
能源大数据分析 浏览:803
长沙网络技术培训机构 浏览:673
三大数据库简介 浏览:343
学编程有哪些好的软件 浏览:168
西门子s7300编程电缆哪个好用 浏览:197
jsp中script标签 浏览:483
linuxdns测试工具 浏览:19
学生用什么编程 浏览:27
苹果怎么看缓存的文件在哪里 浏览:785
用光盘引导linux系统安装教程 浏览:956
ps如何将元素复制到另外一个文件 浏览:651
win10删除文件怎么用管理员权限 浏览:941
qq监控文件删除 浏览:58
java设置下载文件的名字 浏览:48
iphone6屏幕出现平行竖条纹 浏览:741
教育大数据应用典型示范项目 浏览:934
有什么好的消化内科科普网站 浏览:504
打开网络映射存储为什么特别慢 浏览:157
苹果手机摔弯了还能修吗 浏览:532

友情链接