Problem Description:
For different browsers exist on the Chinese encoding format problem, in the export of Excel file, the Chinese file name garbled, that is, in the program to export the file to specify a Chinese name, the browser appears in the Download box file name appears garbled, resolved as follows:
Solution:
[Java]View PlainCopy
- Date dt=new Date (); If no format is required, the current system time can be used directly with Dt,dt
- DateFormat df = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss"); Set display format
- String nowtime="";
- Nowtime= df.format (DT); //Use the format () method of DateFormat to get in DT and display in YYYY/MM/DD HH:MM:SS format
- Writableworkbook book = null;
- String fileName = null;
- filename = "Chinese file name" +nowtime+ ". xls";
- OutputStream OS = null;
- final String useragent = Request.getheader ("user-agent");
- try {
- OS = Response.getoutputstream ();
- Esponse.reset (); //Clear output stream
- String finalfilename = null;
- if (Stringutils.contains (useragent, "MSIE")) {//ie browser
- Finalfilename = Urlencoder.encode (FileName,"UTF8");
- }Else if (Stringutils.contains (useragent, "Mozilla")) {//google, Firefox browser
- Finalfilename = New String (Filename.getbytes (), "iso8859-1");
- }else{
- Finalfilename = Urlencoder.encode (FileName,"UTF8"); Other browsers
- }
- Response.setheader ("content-disposition", "attachment; Filename=\ "" + finalfilename + "\" "); //Set up here to let the browser pop up the download prompt instead of opening it directly in the browser
- Response.setcontenttype ("application/vnd.ms-excel");
- } catch (Unsupportedencodingexception e) {
- E.printstacktrace ();
- }catch (IOException e) {
- E.printstacktrace ();
- }
- Book = Workbook.createworkbook (OS);
- Writablesheet sheet = book.createsheet ("sheet_1", 0), ......... ... .... .........
Exporting Excel file names between different browsers in the Java Web garbled problem Solution