導出excel,格式設置


package com.java.demo;  
  
import java.io.File;  
import java.io.IOException;  
import jxl.Workbook;  
import jxl.format.Alignment;  
import jxl.format.Border;  
import jxl.format.BorderLineStyle;  
import jxl.format.Colour;  
import jxl.format.UnderlineStyle;  
import jxl.format.VerticalAlignment;  
import jxl.write.Label;  
import jxl.write.WritableCellFormat;  
import jxl.write.WritableFont;  
import jxl.write.WritableSheet;  
import jxl.write.WritableWorkbook;  
import jxl.write.WriteException;  
import jxl.write.biff.RowsExceededException;  
  
/** 
 * 導出Excel實例: 
 * @author Administrator 
 * 
 */  
public class ExcelDemo {  
  
    /** 
     * @param args 
     * @throws IOException  
     * @throws WriteException  
     * @throws RowsExceededException  
     */  
    public static void main(String[] args) throws IOException, RowsExceededException, WriteException {  
        //創建Excel工作簿;  
        WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/ExcelDemo.xls"));  
  
        //創建Excel電子薄;  
        WritableSheet sheet = workbook.createSheet("第一個Sheet", 0);  
        //分別給2,3,4列設置不同的寬度;  
        sheet.setColumnView(1, 40);  
        sheet.setColumnView(1, 30);  
        sheet.setColumnView(2, 50);  
        sheet.setColumnView(3, 20);  
  
        //給sheet電子版中所有的列設置默認的列的寬度;  
        sheet.getSettings().setDefaultColumnWidth(30);  
  
        //設置字體;  
        WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.RED);  
  
        WritableCellFormat cellFormat1 = new WritableCellFormat(font1);  
        //設置背景顏色;  
        cellFormat1.setBackground(Colour.BLUE_GREY);  
        //設置邊框;  
        cellFormat1.setBorder(Border.ALL, BorderLineStyle.DASH_DOT);  
        //設置自動換行;  
        cellFormat1.setWrap(true);  
        //設置文字居中對齊方式;  
        cellFormat1.setAlignment(Alignment.CENTRE);  
        //設置垂直居中;  
        cellFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);  
        //創建單元格  
        Label label1 = new Label(0, 0, "第一行第一個單元格(測試是否自動換行!)",cellFormat1);  
        Label label2 = new Label(1, 0, "第一行第二個單元格",cellFormat1);  
        Label label3 = new Label(2, 0, "第一行第三個單元格",cellFormat1);  
        Label label4 = new Label(3, 0, "第一行第四個單元格",cellFormat1);  
        //添加到行中;  
        sheet.addCell(label1);  
        sheet.addCell(label2);  
        sheet.addCell(label3);  
        sheet.addCell(label4);  
          
        //給第二行設置背景、字體顏色、對齊方式等等;  
        WritableFont font2 = new WritableFont(WritableFont.ARIAL,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE2);  
        WritableCellFormat cellFormat2 = new WritableCellFormat(font2);  
        cellFormat2.setAlignment(Alignment.CENTRE);  
        cellFormat2.setBackground(Colour.PINK);  
        cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);  
        cellFormat2.setWrap(true);  
  
        //創建單元格;  
        Label label11= new Label(0,  1, "第二行第一個單元格(測試是否自動換行!)",cellFormat2);  
        Label label22 = new Label(1, 1, "第二行第二個單元格",cellFormat2);  
        Label label33 = new Label(2, 1, "第二行第三個單元格",cellFormat2);  
        Label label44 = new Label(3, 1, "第二行第四個單元格",cellFormat2);  
     sheet.mergeCells(index+7,10,index+8,10); //添加合並單元格,第一個參數是起始列,第二個參數是起始行,第三個參數是終止列,第四個參數是終止行 sheet.addCell(label11); sheet.addCell(label22); sheet.addCell(label33); sheet.addCell(label44);
//寫入Excel表格中; workbook.write(); //關閉流; workbook.close(); } }

 1、controller

 

public String exportStaffAttendce(AttendMonthly domain, Model model, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirect) {try {    
            String fileName = "員工考勤.xls";
            response.setContentType("application/x-msdownload");
            if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
                fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
            }
            //IE
            else{
                fileName = URLEncoder.encode(fileName, "UTF-8");
            }
            response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\"");
            try {
                attendMonthlyService.dailyPerQueryExport(domain, response.getOutputStream());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

2、service

public void dailyPerQueryExport(AttendMonthly domain, OutputStream os)
            throws RowsExceededException, WriteException, IOException, Exception  {//創建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(os);
        //創建新的一頁
        WritableSheet sheet = workbook.createSheet("人員日志", 0);
        //構造表頭
        sheet.mergeCells(0, 0, size*11-1, 2);//添加合並單元格,第一個參數是起始列,第二個參數是起始行,第三個參數是終止列,第四個參數是終止行
        //生成一個單元格樣式控制對象
        WritableCellFormat titleFormate = new WritableCellFormat();
        titleFormate.setAlignment(jxl.format.Alignment.CENTRE);//單元格中的內容水平方向居中
        titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//單元格的內容垂直方向居中
        titleFormate.setWrap(true);
        //設置字體;  
        WritableFont font1 = new WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
        //生成一個單元格樣式控制對象
        WritableCellFormat titleFormate2 = new WritableCellFormat(font1);
        titleFormate2.setAlignment(jxl.format.Alignment.CENTRE);//單元格中的內容水平方向居中
        titleFormate2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//單元格的內容垂直方向居中
        titleFormate2.setWrap(true);
        //設置邊框;  
        titleFormate.setBorder(Border.ALL, BorderLineStyle.THIN);
        Label title = new Label(0,0,"員工考勤表"+"\r\n"+"考勤日期:"+begin + "~" + end+"      "+"制表時間:"+create,titleFormate2);
        sheet.addCell(title);
        
        //給sheet電子版中所有的列設置默認的列的寬度;  
        sheet.getSettings().setDefaultColumnWidth(4);
        
        //創建要顯示的具體內容
        //表頭
        for(int i=0;i<size;i++){
            int index = i*11;
            //分別給1,2列設置不同的寬度; 
            sheet.setColumnView(index, 3);  // 日期
            sheet.setColumnView(index+1, 3);  // 星期
            sheet.setColumnView(index+6, 8);  
            AttendMonthly am = list.get(i);
            // 第一行
            Label deptName = new Label(index,3,"部門",titleFormate);
            sheet.addCell(deptName);
            sheet.mergeCells(index,3,index+1,3);
            Label deptNaTd = new Label(index+2,3,am.getOrgName(),titleFormate);
            sheet.addCell(deptNaTd);
            sheet.mergeCells(index+2,3,index+6,3);
            Label name = new Label(index+7,3,"姓名",titleFormate);
            sheet.addCell(name);
            Label nameTd = new Label(index+8,3,am.getStaffName(),titleFormate);
            sheet.addCell(nameTd);
            sheet.mergeCells(index+8,3,index+10,3);
            // 第二行
            Label date = new Label(index,4,"日期",titleFormate);
            sheet.addCell(date);
            sheet.mergeCells(index,4,index+1,4);
            Label dateTd = new Label(index+2,4,begin + "~" + end,titleFormate);
            sheet.addCell(dateTd);
            sheet.mergeCells(index+2,4,index+6,4);
            Label num = new Label(index+7,4,"工號",titleFormate);
            sheet.addCell(num);
            Label numTd = new Label(index+8,4,am.getStaffNo(),titleFormate);
            sheet.addCell(numTd);
            sheet.mergeCells(index+8,4,index+10,4);
        }
        for(int i=0;i<dates.length;i++){
            String tm = dates[i];
            String subTm = tm.substring(8, 10);
            int row = i + 11;
            for(int j=0;j<list.size();j++){
                AttendMonthly am = list.get(j);
                int cols = j*11;
                for(int n=0;n<attendanceList.size();n++){
                    AttendRecord aR = attendanceList.get(n);
                    if(am.getStaffId() == aR.getStaffId()){
                        if(!StringUtil.isEmptyString(aR.getAttendDate()) && aR.getAttendDate().equals(tm)){
                            Label dat = new Label(cols,row,subTm,titleFormate);// 日期
                            sheet.addCell(dat);
                            Label weekNum = new Label(cols+1,row,aR.getWeek(),titleFormate);// 星期
                            sheet.addCell(weekNum);
                            Label amIn = new Label(cols+2,row,aR.getInRegisterTime(),titleFormate);// 上午上班
                            sheet.addCell(amIn);
                            sheet.mergeCells(cols+2,row,cols+3,row);
                            Label amOut = new Label(cols+4,row,aR.getAmOutRegisterTime(),titleFormate);// 上午下班
                            sheet.addCell(amOut);
                            sheet.mergeCells(cols+4,row,cols+5,row);
                            Label pmIn = new Label(cols+6,row,aR.getPmInRegisterTime(),titleFormate);// 下午上班
                            sheet.addCell(pmIn);
                            Label pmOut = new Label(cols+7,row,aR.getOutRegisterTime(),titleFormate);// 下午下班
                            sheet.addCell(pmOut);
                            sheet.mergeCells(cols+7,row,cols+8,row);
                            Label signIn = new Label(cols+9,row,"",titleFormate);// 簽到
                            sheet.addCell(signIn);
                            Label signOut = new Label(cols+10,row,"",titleFormate);// 簽退
                            sheet.addCell(signOut);
                        }
                    }
                }
            }
        }
        //把創建的內容寫入到輸出流中,並關閉輸出流
        workbook.write();
        workbook.close();
        os.flush();
        os.close();
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM