搜尋此網誌

2017年3月15日 星期三

(JAVA)寫檔案並直接下載(txt)

        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HH_mm");
            String filename = sf.format(new Date()).toString() + "_Export.txt";

//可直接下載檔案
          String filepath = filename;
            response.reset();
            response.setContentType("application/octet-stream");
            String fileName = URLDecoder.decode(filepath, "utf-8");
            java.net.URLEncoder.encode(fileName, "utf-8");
            //response.addHeader("Content-Disposition", "attachment;" + "filename=\"" + URLEncoder.encode(fileName, "utf-8") + "\"");
            response.setHeader("Content-Disposition", "attachment;filename=" + filename);

(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();