首页编程java编程java 为什么导出excel,java代码怎么导出excel文件

java 为什么导出excel,java代码怎么导出excel文件

编程之家2023-10-11118次浏览

大家好,感谢邀请,今天来为大家分享一下java 为什么导出excel的问题,以及和java代码怎么导出excel文件的一些困惑,大家要是还不太明白的话,也没有关系,因为接下来将为大家分享,希望可以帮助到大家,解决大家的问题,下面就开始吧!

java 为什么导出excel,java代码怎么导出excel文件

java excel导出到用户本地

一般来说做下载功能的确是先导到服务器的一个临时目录上的,然后再用一段代码把这个excel读出来,并且输出到response流里面去,给你一段可以用的代码

//------------------------------

//step1.保存一个临时excel到temp目录下

java 为什么导出excel,java代码怎么导出excel文件

//------------------------------

//这部分自己实现,我相信你已经实现了

//假设你已经实现了保存一个excel到一个临时文件夹里面去

java 为什么导出excel,java代码怎么导出excel文件

//并且已经生成了一个File指向这个临时的excel,名叫exportFile

//-------------------------------

//step2.弹出下载对话框

//-------------------------------

if(exportFile==null){

logger.error("生成excel错误!exportFile为空");

return;

}

//先建立一个文件读取流去读取这个临时excel文件

FileInputStreamfs=null;

try{

fs=newFileInputStream(exportFile);

}catch(FileNotFoundExceptione){

logger.error("生成excel错误!"+exportFile+"不存在!",e);

return;

}

//设置响应头和保存文件名

HttpServletResponseresponse=ServletActionContext.getResponse();

//这个一定要设定,告诉浏览器这次请求是一个下载的数据流

response.setContentType("APPLICATION/OCTET-STREAM");

try{

//这边的"采购部门本月采购报表.xls"替换成你自己要显示给用户的文件名

excelName=URLEncoder.encode("采购部门本月采购报表.xls","UTF-8");

}catch(UnsupportedEncodingExceptione1){

logger.error("转换excel名称编码错误!",e1);

}

response.setHeader("Content-Disposition","attachment;filename=\""+excelName+"\"");

//写出流信息

intb=0;

try{

//这里的response就是你servlet的那个传参进来的response

PrintWriterout=response.getWriter();

while((b=fs.read())!=-1){

out.write(b);

}

fs.close();

out.close();

logger.debug(sheetName+"文件下载完毕.");

}catch(Exceptione){

logger.error(sheetName+"下载文件失败!.",e);

}

把这段代码放到你的servlet的最后一部分

java代码怎么导出excel文件

excel工具类

package com.ohd.ie.product.action;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import org.apache.commons.io.output.ByteArrayOutputStream;

import jxl.Workbook;

import jxl.format.Alignment;

import jxl.format.VerticalAlignment;

import jxl.write.*;

import jxl.write.Number;

import jxl.write.biff.RowsExceededException;

public class Excel{

private OutputStream os;

private WritableWorkbook wwb= null;

private WritableSheet ws= null;

private WritableCellFormat titleCellFormat= null;

private WritableCellFormat noBorderCellFormat= null;

private WritableCellFormat hasBorderCellFormat= null;

private WritableCellFormat hasBorderCellNumberFormat= null;

private WritableCellFormat hasBorderCellNumberFormat2= null;

private WritableImage writableImage=null;

private int r;

public Excel(OutputStream os){

this.os= os;

r=-1;

try{

wwb= Workbook.createWorkbook(os);

//创建工作表

ws= wwb.createSheet("sheet1",0);

//设置表头字体,大小,加粗

titleCellFormat= new WritableCellFormat();

titleCellFormat.setAlignment(Alignment.CENTRE);

titleCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

//自动换行

titleCellFormat.setWrap(true);

titleCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12,WritableFont.BOLD));

titleCellFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

//设置表格字体,大小----无边框

noBorderCellFormat= new WritableCellFormat();

noBorderCellFormat.setAlignment(Alignment.CENTRE);

noBorderCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

noBorderCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));

//设置表格字体,大小----有边框

hasBorderCellFormat= new WritableCellFormat();

hasBorderCellFormat.setAlignment(Alignment.CENTRE);

hasBorderCellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

hasBorderCellFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));

hasBorderCellFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

//设置表格字体,大小----有边框(小数)

NumberFormat nf= new NumberFormat("#0.00");

hasBorderCellNumberFormat= new WritableCellFormat(nf);

hasBorderCellNumberFormat.setAlignment(Alignment.CENTRE);

hasBorderCellNumberFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

hasBorderCellNumberFormat.setFont(new WritableFont(WritableFont.createFont("宋体"),12));

hasBorderCellNumberFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

//设置表格字体,大小----有边框(整数)

NumberFormat nf2= new NumberFormat("#0");

hasBorderCellNumberFormat2= new WritableCellFormat(nf2);

hasBorderCellNumberFormat2.setAlignment(Alignment.CENTRE);

hasBorderCellNumberFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);

hasBorderCellNumberFormat2.setFont(new WritableFont(WritableFont.createFont("宋体"),12));

hasBorderCellNumberFormat2.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);

} catch(Exception e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

/**

*

*@param content内容

*@param c列

*@param style样式

*@param isNewLine是否换行

*@param mergeType合并类型

*@param mergeCount合并个数

*@param width单元格宽

*/

public void setExcelCell(String content,int c,int style,boolean isNewLine,int mergeType,int mergeCount,int width){

try{

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////报表内容////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

if(isNewLine){

r++;

}

WritableCell l= null;

if(style== 1){

l= new Label(c,r,content,titleCellFormat);

}

else if(style== 2){

l= new Label(c,r,content,noBorderCellFormat);

}

else if(style== 3){

l= new Label(c,r,content,hasBorderCellFormat);

}

else if(style== 4){

l= new Number(c,r,Double.parseDouble(content),hasBorderCellNumberFormat);

}

else if(style== 5){

l= new Number(c,r,Integer.parseInt(content),hasBorderCellNumberFormat2);

}

ws.addCell(l);

if(width!= 0){

ws.setColumnView(c,width);

}

//veryhuo,com

if(mergeType== 1){

//x轴方向

ws.mergeCells(c, r, c+mergeCount-1, r);

}

else if(mergeType== 2){

//y轴方向

ws.mergeCells(c, r, c, r+mergeCount-1);

}

if(isNewLine){

ws.setRowView(r, 350);

if(style== 1&& r!= 0){

ws.setRowView(r, 900);

}

else{

ws.setRowView(r, 350);

}

}

//

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

} catch(Exception e){

System.out.println(e.toString());

}

}

public void setExcelCellEx(String content,int c,int style,boolean isNewLine,int mergeType,int mergeCount,int width,int row){

try{

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////报表内容////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

if(isNewLine){

r++;

}

WritableCell l= null;

if(style== 1){

l= new Label(c,r,content,titleCellFormat);

}

else if(style== 2){

l= new Label(c,r,content,noBorderCellFormat);

}

else if(style== 3){

if(content.indexOf(".jpg")!=-1||content.indexOf(".JPG")!=-1){

File outputFile=null;

File imgFile=new File(content);

if(imgFile.exists()&&imgFile.length()>0){

BufferedImage input=null;

try{

input= ImageIO.read(imgFile);

} catch(Exception e){

e.printStackTrace();

}

if(input!=null){

String path=imgFile.getAbsolutePath();

outputFile= new File(path.substring(0,path.lastIndexOf('.')+1)+"png");

ImageIO.write(input,"PNG", outputFile);

if(outputFile.exists()&&outputFile.length()>0){

ws.setRowView(row,2000);

//ws.setColumnView(8, 10);

writableImage= new WritableImage(c+0.1, row+0.1, 0.8, 0.8, outputFile);

ws.addImage(writableImage);

l= new Label(c,r,"",hasBorderCellFormat);

}

}

}

}else{

l= new Label(c,r,content,hasBorderCellFormat);

}

}

else if(style== 4){

l= new Number(c,r,Double.parseDouble(content),hasBorderCellNumberFormat);

}

else if(style== 5){

l= new Number(c,r,Integer.parseInt(content),hasBorderCellNumberFormat2);

}

ws.addCell(l);

if(width!= 0){

ws.setColumnView(c,width);

}

if(mergeType== 1){

//x轴方向

ws.mergeCells(c, r, c+mergeCount-1, r);

}

else if(mergeType== 2){

//y轴方向

ws.mergeCells(c, r, c, r+mergeCount-1);

}

if(isNewLine){

ws.setRowView(r, 350);

if(style== 1&& r!= 0){

ws.setRowView(r, 900);

}

else{

ws.setRowView(r, 350);

}

}

} catch(Exception e){

System.out.println(e.toString());

}

}

public void setRowHeight(int val){

try{

ws.setRowView(r, val);

} catch(RowsExceededException e){

e.printStackTrace();

}

}

public void getExcelResult(){

try{

wwb.write();

} catch(Exception e){

System.out.println(e.toString());

}

finally{

if(wwb!= null){

try{

wwb.close();

if(os!= null){

os.close();

}

} catch(WriteException e){

e.printStackTrace();

} catch(IOException e){

e.printStackTrace();

}

}

}

}

}

需要的jar包:jxl.jar

java导出excel时用了excel中内置函数SUM的问题!

里面的公式不复杂,原理复杂,关键在于要掌握它的实质问题,这样用起来才不会出错。同样,一个很简单的函数运用起来也很复杂,我自编的这个金额统计的你看下:分项=RIGHT(SUM(W5:W14),1);角项=RIGHT(SUM(V5:V14)+INT((SUM(W5:W14)/10)));元项=RIGHT(SUM(U5:U14)+INT((SUM(V5:V14)+INT((SUM(W5:W14)/10)))/10)),十项=RIGHT(SUM(T5:T14)+INT((SUM(U5:U14)+INT((SUM(V5:V14)+INT((SUM(W5:W14)/10)))/10))/10)),…,万项=SUM(Q5:Q14)+INT((SUM(R5:R14)+INT((SUM(S5:S14)+INT((SUM(T5:T14)+INT((SUM(U5:U14)+INT((SUM(V5:V14)+INT((SUM(W5:W14)/10)))/10))/10))/10))/10))/10),其功能是统计该列总和,并将十位数前的数值进位到下一项,最终该项只保留个位数字。

关于java 为什么导出excel的内容到此结束,希望对大家有所帮助。

javascript闭包是什么?什么是Javascript的闭包哪吒电影(哪吒电影观后感50字)