|
ExportConsumDailyManagementByExcelDao exportConsumDailyManagementByExcelDao=new ExportConsumDailyManagementByExcelDaoImpl();
/**
* 构造函数
*/
public ExportConsumDailyManagementByExcelServlet() {
super();
}
/**
*销毁
*/
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response){
String AUSE_DEPARTMENT = SessionUtils.getSysUserDept(request);
String ADIV_ID=SessionUtils.getSysDivID(request);
response.reset();
response.setContentType("application/vnd.ms-excel");
String fileName = null;
try {
fileName = new String("ConsumablesBaseInfo.xls".getBytes("gb2312")); //文件名
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setHeader("Content-Disposition", "attachment;filename="+fileName ); //设置头
try {
writeExcel(response.getOutputStream(),getExeclContent(AUSE_DEPARTMENT,ADIV_ID),getExeclTitle(),AUSE_DEPARTMENT); //调用下面写的方法
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response){
response.setContentType("text/HTML");
try {
PrintWriter out = response.getWriter();
} catch (IOException e) {
e.printStackTrace();
}
doGet(request,response);
}
/**
* 输出Excel
* @param os ----输出流
* @param list ----列表
* @param title ----标题
* @param ms_subject_no ---
* @throws Exception
*/
public static void writeExcel(OutputStream outputStream, List list, String title,String AUSE_DEPARTMENT) throws Exception {
jxl.write.WritableWorkbook writableWorkbook = Workbook.createWorkbook(outputStream); // 建立excel文件
jxl.write.WritableSheet writableSheet = writableWorkbook.createSheet("第一页", 0); // sheet名称
WritableFont writableFont = new WritableFont(WritableFont.COURIER, 15, WritableFont.BOLD , false); //设置字体格式为excel支持的格式
WritableFont writableFont2 = new WritableFont(WritableFont.COURIER, 10, WritableFont.BOLD , false); //设置字体格式为excel支持的格式
jxl.write.Label labelC = null; // Excel表格的Cell
WritableCellFormat totalx2Format = new WritableCellFormat(writableFont);
totalx2Format.setVerticalAlignment(VerticalAlignment.CENTRE);
totalx2Format.setAlignment(Alignment.CENTRE);
WritableCellFormat totalx3Format = new WritableCellFormat(writableFont2);
totalx3Format.setVerticalAlignment(VerticalAlignment.CENTRE);
totalx3Format.setAlignment(Alignment.RIGHT); //右对齐
WritableCellFormat totalx4Format = new WritableCellFormat(writableFont2);
totalx4Format.setVerticalAlignment(VerticalAlignment.CENTRE);
totalx4Format.setAlignment(Alignment.LEFT); //左对齐
WritableCellFormat totalx5Format = new WritableCellFormat(writableFont2);
totalx5Format.setVerticalAlignment(VerticalAlignment.CENTRE);
totalx5Format.setAlignment(Alignment.CENTRE); //剧中对齐
totalx5Format.setWrap(true);
WritableCellFormat totalx6Format = new WritableCellFormat();
totalx6Format.setVerticalAlignment(VerticalAlignment.CENTRE);
totalx6Format.setAlignment(Alignment.LEFT); //左对齐
totalx6Format.setWrap(true);
String keshi = "------:"+AUSE_DEPARTMENT;
String time ="时间:"+DateUtils.getCurDate(); //获取当前时间
String title2 = "------------";
writableSheet.mergeCells(0, 0,11, 3);
Label sheetTitle = new Label(0, 0, title2, totalx2Format); //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(sheetTitle);
writableSheet.setColumnView(0,130); //参数含义:(序号,单元格宽度)
labelC = new jxl.write.Label(0, 4, keshi,totalx4Format); //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(2,130); //参数含义:(序号,单元格宽度)
labelC = new jxl.write.Label(11, 4, time,totalx3Format); //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
/**
* 设置标题
*/
String[] str = title.split(","); //将获取的标题字符串进行分割;
writableSheet.setColumnView(0,10); //参数含义:(序号,单元格宽度)
labelC = new jxl.write.Label(0, 6, str[0],totalx5Format); //标题1 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(1,20);
labelC = new jxl.write.Label(1, 6, str[1],totalx5Format); //标题2 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(2,30);
labelC = new jxl.write.Label(2, 6, str[2],totalx5Format); //标题3 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(3,30);
labelC = new jxl.write.Label(3, 6, str[3],totalx5Format); //标题4 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(4,20);
labelC = new jxl.write.Label(4, 6, str[4],totalx5Format); //标题5 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(5,10);
labelC = new jxl.write.Label(5, 6, str[5],totalx5Format); //标题6 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(6,20);
labelC = new jxl.write.Label(6, 6, str[6],totalx5Format); //标题7 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(7,20);
labelC = new jxl.write.Label(7, 6, str[7],totalx5Format); //标题8 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(8,30);
labelC = new jxl.write.Label(8, 6, str[8],totalx5Format); //标题9 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(9,15);
labelC = new jxl.write.Label(9, 6, str[9],totalx5Format); //标题10 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(10,30);
labelC = new jxl.write.Label(10, 6, str[10],totalx5Format); //标题11 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
writableSheet.setColumnView(10,30);
labelC = new jxl.write.Label(11, 6, str[11],totalx5Format); //标题12 //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
if (list != null) {
for (int i = 0; i < list.size(); i++) {
String tmp = (String)list.get(i);
String[] row = tmp.split("&");
if(row!=null&&row.length>0){
for (int j = 0; j < row.length; j++) {
labelC = new jxl.write.Label(j, i +1+6, row[j],totalx6Format); //参数含义:(在第几列,在第几行,列名,单元格格式)
writableSheet.addCell(labelC);
}
}
}
}
writableWorkbook.write();
writableWorkbook.close();
}
/**
* 获取Excel内容
* @param condition
* @return
*/
public List getExeclContent(String AUSE_DEPARTMENT,String ADIV_ID){
List result = new ArrayList();
List list= new LinkedList();
if(AUSE_DEPARTMENT==null||AUSE_DEPARTMENT.length()==0) return list;
if(ADIV_ID==null||ADIV_ID.length()==0) return list;
Connection conn;
StringBuffer ConsumablesDailyManagementSB=new StringBuffer();
ConsumablesDailyManagementSB.append("select * from ast_consumables_base_info t where 1=1 ");
if(ADIV_ID!=null&&ADIV_ID!=""){
ConsumablesDailyManagementSB.append(" and t.ADIV_ID='"+ADIV_ID+"'");
}
if(AUSE_DEPARTMENT!=null&&AUSE_DEPARTMENT!=""){
ConsumablesDailyManagementSB.append(" and t.AUSE_DEPARTMENT='"+AUSE_DEPARTMENT+"'");
}
ConsumablesDailyManagementSB.append(" ORDER BY CONSUMABLES_ID DESC");
String sqlString=ConsumablesDailyManagementSB.toString();
try{
conn=ApplicationConnSource.getConnection();
result=exportConsumDailyManagementByExcelDao.SelectConsumablesByExport(conn, sqlString); //执行dao层方法;
DbUtils.close(conn);
int num = 1;
for(int i=0;i<result.size();i++){
ConsumblesBaseInfo consumblesBaseInfo= (ConsumblesBaseInfo)result.get(i);
String ACONSUMABLES_CODE=consumblesBaseInfo.getACONSUMABLES_CODE();
String RECIPIENTS_STATE=consumblesBaseInfo.getRECIPIENTS_STATE();
String ACONSUMABLES_NAME=consumblesBaseInfo.getACONSUMABLES_NAME();
String MEASURE_UNIT=consumblesBaseInfo.getMEASURE_UNIT();
String AMODEL_DESC=consumblesBaseInfo.getAMODEL_DESC();
Double ACURR_VALUE=consumblesBaseInfo.getACURR_VALUE();
String AGET_DATE=consumblesBaseInfo.getAGET_DATE();
String FMANAGE_DIV=consumblesBaseInfo.getFMANAGE_DIV();
String CREATE_USER=consumblesBaseInfo.getCREATE_USER();
String FPROP_PAPER_CODE=consumblesBaseInfo.getFPROP_PAPER_CODE();
String INSTORE_DATE=consumblesBaseInfo.getINSTORE_DATE();
if(RECIPIENTS_STATE=="1"||RECIPIENTS_STATE.equals("1")){
RECIPIENTS_STATE="--------";
}
if(RECIPIENTS_STATE=="2"||RECIPIENTS_STATE.equals("2")){
RECIPIENTS_STATE="---------";
}
//格式化
DecimalFormat df = new DecimalFormat("###############0.00");// 16位整数位,两小数位
String ACURR_Value= df.format(ACURR_VALUE);
String ret = String.valueOf(num)+"&"+ACONSUMABLES_CODE+"&"+RECIPIENTS_STATE+"&"+ACONSUMABLES_NAME+"&"+MEASURE_UNIT+"&"+AMODEL_DESC+"&"
+String.valueOf(ACURR_Value)+"&"+AGET_DATE+"&"+FMANAGE_DIV+"&"+CREATE_USER+"&"+FPROP_PAPER_CODE+"&"+INSTORE_DATE;
list.add(ret); //将每条记录添加到列表中
num++;
}
return list;
}catch(SQLException e){
e.printStackTrace();
return null;
}
}
public String getExeclTitle(){
String title = "-----,------,------,------,-------,-------,------,--------,-------,--------,---------,--------";
return title;
}
//“-----“为需要的标题
/**
* 初始化
*/
public void init() throws ServletException {
} |
|