首页 > 代码库 > java中使用jxl实现表格导出
java中使用jxl实现表格导出
WritableWorkbook book = null;
response.reset();
//PageContext.
response.setCharacterEncoding("UTF-8");// 设置字符集
// 设置带有字形formatting的对象标题字体
// WritableFont.BOLD, true);
WritableFont wf14 = new WritableFont(WritableFont.createFont("微软雅黑"), 14,
WritableFont.BOLD, false);
WritableCellFormat wcfF14 = new WritableCellFormat(wf14);
wcfF14.setAlignment(Alignment.CENTRE);//水平对齐
wcfF14.setVerticalAlignment(VerticalAlignment.CENTRE);//垂直对齐
// 标签字体属性
WritableFont wf12 = new WritableFont(WritableFont.createFont("微软雅黑"), 11,
WritableFont.BOLD, false);
WritableCellFormat wcfF12 = new WritableCellFormat(wf12);
wcfF12.setAlignment(Alignment.CENTRE);
wcfF12.setVerticalAlignment(VerticalAlignment.CENTRE);
// 内容字体属性
WritableFont wf11 = new WritableFont(WritableFont.createFont("微软雅黑"), 11,
WritableFont.NO_BOLD, false);
WritableCellFormat wcfF11 = new WritableCellFormat(wf11);
wcfF11.setAlignment(Alignment.CENTRE);
// 创建人审核人字体属性
WritableFont wf13 = new WritableFont(WritableFont.createFont("微软雅黑"), 8,
WritableFont.NO_BOLD, false);
WritableCellFormat wcfF13 = new WritableCellFormat(wf11);
// 设置边框给单元格加边框
wcfF12.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
// 创建工作流
OutputStream os = null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
response.setCharacterEncoding("UTF-8");
// 设置工作表的标题
// response.setHeader("Content-Disposition",
// "attachment; filename=Norecord_Social_Credit_Code.xls");
// 设置生成的文件名字
response.addHeader("Content-Disposition", "inline; filename="
+ new String(name.getBytes("GB2312"), "ISO8859_1") + ".xls");
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
//logger.error("导出excel出现IO异常", e1);
throw new ServiceException("导出失败", e1);
}
try {
// 设置日期格式
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");//日期格式1
SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//日期格式1
Timestamp appDate = order_scrk.getB_createTime();
String appDateStr = sf.format(appDate);//建表日期
//String product_date=sf2.format(order_cgtl_dts.getProduct_date());
// 以下为excel表格内容
// int nCount = list.size();
WritableSheet sheet = book.createSheet(name, 0);// 设置sheet名称
sheet.getSettings().setAutomaticFormulaCalculation(true);
// 设置默认列宽
sheet.getSettings().setDefaultColumnWidth(14);
// 设置默认行高
// sheet.getSettings().setDefaultRowHeight(300);
// 生成名工作表,参数0表示这是第一页
// int nI = 1;
// 表字段名
sheet.mergeCells(0, 0, 11, 0);// 1-k合并
sheet.setRowView(0, 800); // 设置第1行行高
sheet.addCell(new jxl.write.Label(0, 0, name, wcfF14));
// 合并备注那里单元格
sheet.mergeCells(1, 27, 11, 27);//备注那里
sheet.setRowView(27, 1000); // 设置第27行行高
// sheet.setc
sheet.mergeCells(0, 1, 11, 1);// 第二行合并
sheet.addCell(new jxl.write.Label(0, 1, "单据编号:"
+ order_scrk.getNumber() + " \t 日期:" + appDateStr + " \t 单据状态:"
+ order_status.getOrder_status_name(), wcfF12));
sheet.mergeCells(1, 2, 11, 2);// 第3行合并
sheet.addCell(new jxl.write.Label(0, 2, "质检员:", wcfF12));
sheet.addCell(new jxl.write.Label(1, 2, user1.getUser_name(),wcfF11));
sheet.mergeCells(1, 3, 11, 3);// 第4行合并
sheet.addCell(new jxl.write.Label(0, 3, "项目:", wcfF12));
sheet.addCell(new jxl.write.Label(1, 3, project.getProject_name(),wcfF11));//项目名称
//sheet.addCell(new jxl.write.Label(1, 2, "原材料仓", wcfF12));
sheet.addCell(new jxl.write.Label(0, 4, "物料编码", wcfF12));
sheet.addCell(new jxl.write.Label(1, 4, "物料名称", wcfF12));
sheet.addCell(new jxl.write.Label(2, 4, "规格", wcfF12));
sheet.addCell(new jxl.write.Label(3, 4, "型号", wcfF12));
sheet.addCell(new jxl.write.Label(4, 4, "单位", wcfF12));
sheet.addCell(new jxl.write.Label(5, 4, "应收数量", wcfF12));
sheet.addCell(new jxl.write.Label(6, 4, "序列号", wcfF12));
sheet.addCell(new jxl.write.Label(7, 4, "实收数量", wcfF12));
sheet.addCell(new jxl.write.Label(8, 4, "批号", wcfF12));
sheet.addCell(new jxl.write.Label(9, 4, "仓库", wcfF12));
sheet.addCell(new jxl.write.Label(10, 4, "仓位", wcfF12));
sheet.addCell(new jxl.write.Label(11, 4, "库存状态", wcfF12));
for (int i = 0; i < list.size(); i++) {
Order_scrk_dts order_scrk_dts= list.get(i).getOrder_scrk_dts();
String sql="SELECT f.ser_number from finished_p_sernum f"
+" LEFT JOIN order_zj_dts o ON f.order_cskc_dts_id=o.order_zj_dts_id"
+" LEFT JOIN order_scrk_dts s ON o.order_zj_dts_id=s.order_scrk_dts_id"
+" where s.order_scrk_id=?1";
Map<Integer, Object> maps = new HashMap<Integer, Object>();
maps.put(1, order_scrk_dts.getOrder_zj_dts_id());
List<Map<String,Object>> names = baseDao.findBySQL(sql, maps);
String hege="";
for (int j = 0; j < names.size(); j++)
{
hege+=(String) names.get(j).get("ser_number")+",";
}
Material material=list.get(i).getMaterialDTO().getMaterial();//获得材料
//获取单位id
long unit_id=list.get(i).getMaterialDTO().getMaterial().getCom_unit();
Unit unit=(Unit)baseDao.get(Unit.class, (Serializable)unit_id);
//获取仓库
long store_id=order_scrk_dts.getStore_house_id();
Store_house store_house=(Store_house)baseDao.get(Store_house.class, (Serializable)store_id);
//获取仓位
long position=order_scrk_dts.getStore_house_position_id();
Store_house_position house_position=(Store_house_position)baseDao.get(Store_house_position.class, (Serializable)position);
//获取库存状态
long stock=order_scrk_dts.getStock_status();
Stock_status status=(Stock_status)baseDao.get(Stock_status.class, (Serializable)stock);
//获取批次
String batch="";
if(order_scrk_dts.getBatch_num()!=null)
{
batch=order_scrk_dts.getBatch_num().toString();
}
sheet.addCell(new jxl.write.Label(0, 5+i, material.getMaterial_number(),wcfF11));// 编码
sheet.addCell(new jxl.write.Label(1, 5+i, material.getMaterial_name(), wcfF11));//物料名称
sheet.addCell(new jxl.write.Label(2, 5+i, material.getCom_stantard(), wcfF11));//规格
sheet.addCell(new jxl.write.Label(3, 5+i, material.getCom_model(), wcfF11));//型号
sheet.addCell(new jxl.write.Label(4, 5+i, unit.getUnit_name(),wcfF11));//单位
sheet.addCell(new jxl.write.Label(5, 5+i, order_scrk_dts.getYinshou_num().toString(), wcfF11));// 应收数量
sheet.addCell(new jxl.write.Label(6, 5+i, hege,wcfF11));//单位
sheet.addCell(new jxl.write.Label(7, 5+i, order_scrk_dts.getShishou_num().toString(), wcfF11));// 应收数量
sheet.addCell(new jxl.write.Label(8, 5+i, batch, wcfF11));// 应收数量
sheet.addCell(new jxl.write.Label(9, 5+i, store_house.getStore_house_name(), wcfF11));// 应收数量
sheet.addCell(new jxl.write.Label(10, 5+i, house_position.getPosition_name(), wcfF11));// 应收数量
sheet.addCell(new jxl.write.Label(11, 5+i, status.getStock_status_name(), wcfF11));// 应收数量
}
// 28行加备注
sheet.addCell(new jxl.write.Label(0, 27, "备注:", wcfF12));
// 创建人
sheet.addCell(new jxl.write.Label(7, 28, "创建人:"
+ user3.getUser_name(), wcfF13));
// 审核人
sheet.addCell(new jxl.write.Label(8, 28, "审核人:", wcfF13));
sheet.addCell(new jxl.write.Label(1, 27, order_scrk.getRemark(), wcfF11));//备注
book.write();
book.close();
} catch (Exception e) {
throw new ServiceException("导出失败", e);
} finally {
if (null != os) {
try {
os.flush();
os.close();
} catch (IOException e) {
// logger.error("关流出现异常", e);
e.printStackTrace();
}
}
}
java中使用jxl实现表格导出