搜尋此網誌

2017年3月15日 星期三

(JAVA) 產出EXECL的範例


//那些有用到要測試才知道,忘記了
import com.cht.sys.extended.ExtendedDispatchAction;
import com.cht.sys.util.MultiPartReader;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Vector;
import javax.servlet.http.HttpSession;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableWorkbook;

  //開始輸出excel
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");//格式
            String filename = sf.format(new Date()).toString() + "_Export.xls";
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);
            OutputStream os = response.getOutputStream();



Workbook.createWorkbook(File.createTempFile("queryExport",".xls"));//檔名
            WritableWorkbook wwb = Workbook.createWorkbook(os);
            jxl.write.WritableSheet ws = wwb.createSheet("帳號審查表", 0);//名稱,第一個sheet
            ws.setColumnView(0, 20);//後面是欄位長度
            ws.setColumnView(1, 12);

//表頭欄位格式
            WritableFont twfont = new WritableFont(WritableFont.TIMES, 11);
            WritableCellFormat twf = new WritableCellFormat(twfont);
            twf.setBorder(Border.ALL, BorderLineStyle.MEDIUM, Colour.RED);
//第二行開始使用的欄位格式
            WritableFont wfont = new WritableFont(WritableFont.TIMES, 11);
            WritableCellFormat wf = new WritableCellFormat(wfont);

            int col = 0;
            int row = 1;

         
                ws.addCell(new jxl.write.Label(col++, 0, "環境代碼", twf));
                ws.addCell(new jxl.write.Label(col++, 0, "LDAP帳號", twf));

                for (int i = 0; i < sas.length; i++) {
                   //第二行開始寫入值
                    col = 0;
                    ws.addCell(new jxl.write.Label(col++, row, 值, wf));
                    ws.addCell(new jxl.write.Label(col++, row, 值2, wf));

                    row++;
                }

沒有留言:

張貼留言