java 保存excel_java如何另存导出Excel

展开全部

1./**

* 出险信息导出到excel(fc)

* @param mapping

* @param form

* @param request

* @param response

* @throws IOException

*/

public void exportActoExcel(ActionMapping mapping, ActionForm form ,

HttpServletRequest request,HttpServletResponse response) throws IOException {

ActionErrors errors = new ActionErrors();

AcExcelBusi acBusi = new AcExcelBusi();

AccidentRecordForm arForm= (AccidentRecordForm) form;

AccidentRecordBusi arBusi = new AccidentRecordBusi();

// 查询条32313133353236313431303231363533e59b9ee7ad9431333337623439件

FwUsers sessUser = (FwUsers)request.getSession().getAttribute(ConstValues.SESS_USER_MANAGE);

Map cisMap = arBusi.getTodoPageList(arForm,sessUser,errors);

List AcList = null;// 当页的记录

if (null != cisMap) {

AcList = (List) cisMap.get("list");

}

//导出excel的路径、文件名

String uuid = UUID.create("exp");

String path = request.getSession().getServletContext().getRealPath("/") + ConstValues.EXP_PATH_EXCEL + uuid + ".xls";

acBusi.exprotAcExcel(AcList, path,request);

response.sendRedirect("stdownload.jsp?path=" + path );

}

8e1327ddb9f9d06db341621fdbe8598f.png

2./**

* 导出出险信息 fc

*

* @param jzForm

*            查询条件

* @param sessionUser

*            当前登录session用户

* @param errors

*            Action错误

* @return

*/

public void exprotAcExcel(List list, String path,HttpServletRequest request) {

try {

WritableWorkbook workbook = Workbook.createWorkbook(new File(path));// 创建工作簿(filePos为excel文件的路径)

WritableSheet sheet = workbook.createSheet("出险信息", 0);// 创建工作页

// 内容(居中)单元格样式

WritableCellFormat contentStyle = new WritableCellFormat(); //

contentStyle.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

contentStyle.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框

// 一级标题单元格样式

WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 15,

WritableFont.BOLD, false); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat titleStyle1 = new WritableCellFormat(wf1); //

titleStyle1.setBackground(jxl.format.Colour.GREEN); // 设置单元格的背景颜色

titleStyle1.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

titleStyle1.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框

// 二级标题单元格样式

WritableFont wf2 = new WritableFont(WritableFont.ARIAL, 11,

WritableFont.BOLD, false); // 定义格式 字体 下划线 斜体 粗体 颜色

WritableCellFormat titleStyle2 = new WritableCellFormat(wf2); //

titleStyle2.setBackground(jxl.format.Colour.GREY_25_PERCENT); // 设置单元格的背景颜色

titleStyle2.setAlignment(jxl.format.Alignment.CENTRE); // 设置水平对齐方式

titleStyle2

.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); // 设置垂直对齐方式

titleStyle2.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框

WritableCellFormat titleStyle3 = new WritableCellFormat(wf1); //

titleStyle3.setBackground(jxl.format.Colour.BRIGHT_GREEN); // 设置单元格的背景颜色

titleStyle3.setAlignment(jxl.format.Alignment.CENTRE); // 设置对齐方式

titleStyle3.setBorder(jxl.format.Border.ALL,

jxl.format.BorderLineStyle.THIN); // 设置边框

// 设置冻结单元格

sheet.getSettings().setVerticalFreeze(2);

sheet.getSettings().setHorizontalFreeze(5);

sheet.setColumnView(0, 8); // 设置列的宽度

sheet.setColumnView(1, 10); // 设置列的宽度

sheet.setRowView(0, 0);

sheet.addCell(new Label(0, 0, "hysj5800294*#", titleStyle1));// 标题

sheet.addCell(new Label(0, 1, "出险信息 ", titleStyle2));// 标题

sheet.mergeCells(0, 2, 33, 2); // 合并单元格

sheet.addCell(new Label(0, 2, "出险信息 ", titleStyle1));// 标题

sheet.addCell(new Label(0, 3, "序号", titleStyle2));// 往单元格中填写数据

sheet.addCell(new Label(1, 3, "省内/省外", titleStyle2));// 往单元格中填写数据

if (list != null && list.size() > 0) {

for (int i = 0; i < list.size(); i++) {

sheet.setRowView(i + 3, 300); // 设置行高

//转换数据信息

//Object[] obj = (Object[]) list.get(i);

AccidentRecord ar = (AccidentRecord) list.get(i);

sheet.addCell(new Label(0, i + 4, String.valueOf(i + 1),contentStyle));// 往单元格中填写数据 序号

sheet.addCell(new Label(1, i + 4, ar.getLicenceProvince().toString(),contentStyle));//  省内/省外

}

}

workbook.write();// 书写到工作簿

workbook.close();// 关闭工作簿,输出完成

} catch (Exception e) {

e.printStackTrace();

}

}

}

8093e66f03245cbebca54f5112ea85de.png

3.功能可以实现,可以到处,点击下载确定。

2152c842bd2d8ab823edd322e7ae096a.png

4.打开已经下载的excel,如图显示结果。得到想要的结构。

2fb798575b4c78c19ed20a50ab97285b.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值