admin 管理员组

文章数量: 1087139


2024年7月4日发(作者:指令窗口弹窗代码)

下面是本人使用java的poi实现使用模板到处excel,内容包含图片,使用两种不同

的方式实现其到处excel。但是使用jxl实现到处excel只能到处png格式。

package t;

import edImage;

import edInputStream;

import edOutputStream;

import rayOutputStream;

import ;

import putStream;

import tputStream;

import ption;

import O;

import tException;

import tOutputStream;

import rvlet;

import rvletRequest;

import rvletResponse;

import ll;

import ientAnchor;

import triarch;

import w;

import eet;

import rkbook;

import ileSystem;

public class OutExcelServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public

response)

void doGet(HttpServletRequest request, HttpServletResponse

throws ServletException, IOException {

(request, response);

}

public

response)

void doPost(HttpServletRequest request, HttpServletResponse

throws ServletException, IOException {

String accident_id = ameter("accident_id");

String OnputimagePath = null;

/***********************************************使用poi实现excel模版 导出

excel********************/

try {

POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("d:设备异常

分析报告.xls"));

HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet = et("设备异常分析报告");

HSSFRow row = (2);

HSSFCell cell = l((short) 2);

if (cell == null){

cell = Cell((short) 2);

}

lType(_TYPE_STRING);

lValue("12321212");

ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

String InputimagePath = "d:";

BufferedImage bufferImg = (new File(InputimagePath));

(bufferImg,"JPG",byteArrayOut);

//设置图片大小,位置

HSSFClientAnchor anchor = new HSSFClientAnchor(5,0,500,122,(short)

5,(short)10,15);

0,

//创建

HSSFPatriarch patri = DrawingPatriarch();

Picture(anchor

E_TYPE_PNG));

,ture(Array(),

// 输出文件

OnputimagePath = "d:";

FileOutputStream fileOut = new FileOutputStream(OnputimagePath);

(fileOut);

();

} catch (Exception e) {

tackTrace();

}

// 检查文件是否存在

File obj = new File(OnputimagePath);

if (!()) {

tentType("text/html;charset=GBK");

ter().print("指定文件不存在!");

return;

}

// 读取文件名:用于设置客户端保存时指定默认文件名

int index = dexOf(""); // 前提:传入的path字符串以

“”表示目录分隔符

String fileName = ing(index + 1);

// 写流文件到前端浏览器

ServletOutputStream out = putStream();

der("Content-disposition",

fileName);

"attachment;filename=" +

BufferedInputStream bis = null;

BufferedOutputStream bos = null;

try {

bis = new BufferedInputStream(new FileInputStream(OnputimagePath));

bos = new BufferedOutputStream(out);

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = (buff, 0, ))) {

(buff, 0, bytesRead);

}

} catch (IOException e) {

throw e;

} finally {

if (bis != null)

();

if (bos != null)

();

}

}

/*****************使用jxl实现excel模版 导出excel 但导出图片仅支持png格式

**************************************

String realpath = "D:/";

try {

CopeDate data = new CopeDate();

MapList mp = a(accident_id);

//选择模板文件:

Workbook wb = kbook(new File(realpath));

//第二步:通过模板得到一个可写的Workbook:第一个参数是一个输出流对象,第二

个参数代表了要读取的模板

File targetFile = new File("D:/");

WritableWorkbook wwb = Workbook(targetFile, wb);

//第三步:选择模板中名称为设备异常的Sheet:

WritableSheet wws = et("JustCell");

//第四步:选择单元格,写入动态值,根据单元格的不同类型转换成相应类型的单元

格:

Label A1 = new Label(0, 1,(0).get("accident_name"));

l(A1);

//插入图片

File imgFile = new File("D:/");

WritableImage image = new WritableImage(2, 1, 8, 20, imgFile);

ge(image);

();

();

();

} catch (BiffException e) {

// TODO Auto-generated catch block

tackTrace();

} catch (WriteException e) {

// TODO Auto-generated catch block

tackTrace();

} catch (Exception e) {

// TODO Auto-generated catch block

tackTrace();

}

}

****************************************************************************/

}


本文标签: 模板 文件 实现 使用 设置