Exporting Excel file names between different browsers in the Java Web garbled problem Solution

Source: Internet
Author: User
Tags dateformat

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
  1. Date dt=new Date (); If no format is required, the current system time can be used directly with Dt,dt
  2. DateFormat df = new SimpleDateFormat ("Yyyy/mm/dd HH:mm:ss"); Set display format
  3. String nowtime="";
  4. Nowtime= df.format (DT); //Use the format () method of DateFormat to get in DT and display in YYYY/MM/DD HH:MM:SS format
  5. Writableworkbook book = null;
  6. String fileName = null;
  7. filename = "Chinese file name" +nowtime+ ". xls";
  8. OutputStream OS = null;
  9. final String useragent = Request.getheader ("user-agent");
  10. try {
  11. OS = Response.getoutputstream ();
  12. Esponse.reset (); //Clear output stream
  13. String finalfilename = null;
  14. if (Stringutils.contains (useragent, "MSIE")) {//ie browser
  15. Finalfilename = Urlencoder.encode (FileName,"UTF8");
  16. }Else if (Stringutils.contains (useragent, "Mozilla")) {//google, Firefox browser
  17. Finalfilename = New String (Filename.getbytes (), "iso8859-1");
  18. }else{
  19. Finalfilename = Urlencoder.encode (FileName,"UTF8"); Other browsers
  20. }
  21. 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
  22. Response.setcontenttype ("application/vnd.ms-excel");
  23. } catch (Unsupportedencodingexception e) {
  24. E.printstacktrace ();
  25. }catch (IOException e) {
  26. E.printstacktrace ();
  27. }
  28. Book = Workbook.createworkbook (OS);
  29. Writablesheet sheet = book.createsheet ("sheet_1", 0), ......... ... .... .........

Exporting Excel file names between different browsers in the Java Web garbled problem Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.