File Download-excel export-How far can we go series (22)

Source: Internet
Author: User
How far can we go series (22)

Nonsense:Old Rules: Let's talk about it first. To be honest, the Chinese New Year is just like going home to brag, drink, and drink in front of relatives, listen to other people's cows, and then come back to continue suffering ....

So you don't blow others down, but others blow you down. You need to cheer up!

-----------------------------------------

Recently, many people have been entangled in the plagiarism of their articles.

I have been wondering: how much harm does a blog written by someone who has been plagiarized, or even treated as an original author of others?

I don't understand: What are the other losses in addition to some setbacks in my vanity? In addition to some malicious plagiarism, we do have to oppose it.

In my opinion, a sharing activity is magnified and disseminated, which is a nice and cool task.

I personally will also switch to the Article. Generally, the article is signed, and I will also copy it. If I haven't written it, I will also be too lazy to mark where I copied it, but one thing is certain. Although it takes several seconds to copy and paste it, I would like to thank the author. At least I would like to pay off him for a few seconds. I think he taught me something that is invisible, but it may be valuable.

Why do you need to learn how to copy and paste your article?

When you post some thoughts, summaries, and experiences to your blog. I saw my article on another blog one month later. Will it make you angry? This should be a happy thing in the blog garden. It is interesting to think about the fact that my articles have been read and recorded repeatedly and become part of others' thoughts!
It is regarded as a bit of guilt.

What is blog writing really rewarding?

1. Write your own thoughts

2. Rethink about reading your article after writing it.

3. If someone else reads the feedback from your article, then you can expand your thinking.

The whole process is a process that constantly promotes your own thinking. As long as this process can motivate you to think, writing this blog is valuable, at least for yourself.

 

Therefore, I personally think that we should not be too entangled in other people's plagiarism. If others plagiarize, you can only represent others who think this blog has helped him. I think you are good, right.

 

Subject:

1. Download an object

The file is lying in a folder on the server. Click Download on the page to save the file to a local directory:

Action is implemented using springmvc:

Public ModelAndView downloadModelForm (HttpServletRequest request, HttpServletResponse response) throws Exception {response. setContentType ("application/octet-stream"); String fileName = "..docx"; String path = request. getSession (). getServletContext (). getRealPath (BusinessConstants. DOWNLOAD_FILE_PATH + fileName); File file = new File (path); // clear response. reset (); if (request. getHeader ("User-Agent "). toLowerCase (). indexOf ("firefox")> 0) {fileName = new String (fileName. getBytes ("UTF-8"), "ISO8859-1"); // firefox browser} else {fileName = URLEncoder. encode (fileName, "UTF-8"); // other browsers include IE browser and google browser} // set response Header response. addHeader ("Content-Disposition", "attachment; filename =" + fileName); response. addHeader ("Content-Length", "" + file. length (); try {// download the file in the form of a stream InputStream FD = new BufferedInputStream (new FileInputStream (path); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); OutputStream toClient = new BufferedOutputStream (response. getOutputStream (); toClient. write (buffer); toClient. flush (); toClient. close ();} catch (Exception e) {e. printStackTrace ();} return null ;}

Note that the Code is the fileName encoding logic, which implements different Encoding Based on Different browsers. In this way, the Chinese name can be used when the file name is output.

Tests are available for firefox, ie8, and Google. If you do not perform the operation, the result is that the file name uses Chinese characters, and garbled characters may occur. (This may be just a solution. If you have a better solution, please let me know)

 

2. Export the excle file:

The exported content is the bill table. The data needs to be retrieved from the database and assembled into excel.

Here we use the latest pio3.9 (open-source project) to export the excel file:

Service Layer Method: assemble a complete HSSFWorkbook

Public HSSFWorkbook export (List <Sale> list) {HSSFWorkbook wb = new HSSFWorkbook (); HSSFSheet sheet = wb. createSheet (BusinessConstants. EXCEL_SHEET_NAME); HSSFRow row = sheet. createRow (int) 0); HSSFCellStyle style = wb. createCellStyle (); style. setAlignment (HSSFCellStyle. ALIGN_CENTER); String [] excelHeader = BusinessConstants. EXCEL_HEADER; for (int I = 0; I <excelHeader. length; I ++) {HSSFCell cell = row. createCell (I); cell. setCellValue (excelHeader [I]); cell. setCellStyle (style); sheet. autoSizeColumn (I);} SimpleDateFormat sdf = new SimpleDateFormat (BusinessConstants. DATE_FORMAT); for (int I = 0; I <list. size (); I ++) {row = sheet. createRow (I + 1); Sale sale = list. get (I); row. createCell (0 ). setCellValue (sdf. format (sale. getTime (); // The setCellValue method parameter cannot be null, which may cause an exception. Therefore, you must check if (sale. getStrType () = null) {row. createCell (1 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (1 ). setCellValue (sale. getStrType ();} if (sale. getStrPayType () = null) {row. createCell (2 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (2 ). setCellValue ("" + sale. getStrPayType ();} if (sale. getAmount () = null) {row. createCell (3 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (3 ). setCellValue (sale. getAmount ();} if (sale. getProductNum () = null) {row. createCell (4 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (4 ). setCellValue (sale. getProductNum ();} if (sale. getProductValid () = null) {row. createCell (5 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (5 ). setCellValue (sale. getProductValid ();} if (sale. getProductName () = null) {row. createCell (6 ). setCellValue (BusinessConstants. STRIKE);} else {row. createCell (6 ). setCellValue (sale. getProductName () ;}} return wb ;}

 

Code at the action layer:

Public ModelAndView downloadBill (HttpServletRequest request, HttpServletResponse response) throws IOException {User user = (User) request. getSession (). getAttribute ("user"); List <Sale> saleList = accountService. getSale (user. getId (), null, null, 0); HSSFWorkbook wb = accountService. export (saleList); // clear response. reset (); String fileName = ""; if (request. getHeader ("User-Agent "). toLowerCase (). indexOf ("firefox")> 0) {fileName = new String (BusinessConstants. EXCEL_FILE_NAME.getBytes ("UTF-8"), "ISO8859-1"); // firefox browser} else {fileName = URLEncoder. encode (BusinessConstants. EXCEL_FILE_NAME, "UTF-8"); // other browsers include IE browser and google browser} response. setContentType ("application/vnd. ms-excel "); // Office2003 response. setHeader ("Content-disposition", "attachment; filename =" + fileName); OutputStream ouputStream = response. getOutputStream (); wb. write (ouputStream); ouputStream. flush (); ouputStream. close (); return null ;}

Similarly, we need to encode the fileName so that the Chinese name can be used normally.

If the operation is performed, the effect may be as follows:

 

Export results:

 

We hope this will help you.

 

Everybody, Happy New Year!

 

Let's move on

----------------------------------------------------------------------

Hard work may fail, but not hard work will certainly fail.
Mutual encouragement.

Related Article

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.