| | |
| | | package com.gkhy.exam.admin.controller.system; |
| | | |
| | | |
| | | import org.apache.poi.poifs.filesystem.DirectoryEntry; |
| | | import org.apache.poi.poifs.filesystem.DocumentEntry; |
| | | import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.util.Map; |
| | | |
| | | @RestController |
| | | public class WordExportController { |
| | | @ResponseBody |
| | | @RequestMapping(value = "download") |
| | | public int download(HttpServletResponse response, HttpServletRequest request)throws Exception { |
| | | String content = "<h1>标题头</h1><h2>第二个标题</h2><a href=\"www.baidu.com\">百度搜索</a>"; |
| | | StringBuffer sbf = new StringBuffer(); |
| | | sbf.append("<html><body>"); |
| | | sbf.append(content); |
| | | sbf.append("</body></html"); |
| | | exportWord(request,response,String.valueOf(sbf),"word1"); |
| | | return 1; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * @param request |
| | | * @param response |
| | | * @param content 富文本内容 |
| | | * @param fileName 生成word名字 |
| | | * @throws Exception |
| | | */ |
| | | public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception { |
| | | byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。 |
| | | ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中 |
| | | POIFSFileSystem poifs = new POIFSFileSystem(); |
| | | DirectoryEntry directory = poifs.getRoot(); |
| | | DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //该步骤不可省略,否则会出现乱码。 |
| | | //输出文件 |
| | | request.setCharacterEncoding("utf-8"); |
| | | response.setContentType("application/msword");//导出word格式 |
| | | response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"),"iso8859-1") + ".doc"); |
| | | ServletOutputStream ostream = response.getOutputStream(); |
| | | poifs.writeFilesystem(ostream); |
| | | bais.close(); |
| | | ostream.close(); |
| | | poifs.close(); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |
| | | //package com.gkhy.exam.admin.controller.system; |
| | | // |
| | | // |
| | | //import org.apache.poi.poifs.filesystem.DirectoryEntry; |
| | | //import org.apache.poi.poifs.filesystem.DocumentEntry; |
| | | //import org.apache.poi.poifs.filesystem.POIFSFileSystem; |
| | | //import org.springframework.core.io.ClassPathResource; |
| | | //import org.springframework.http.HttpHeaders; |
| | | //import org.springframework.http.MediaType; |
| | | //import org.springframework.http.ResponseEntity; |
| | | //import org.springframework.web.bind.annotation.*; |
| | | // |
| | | //import javax.servlet.ServletOutputStream; |
| | | //import javax.servlet.http.HttpServletRequest; |
| | | //import javax.servlet.http.HttpServletResponse; |
| | | //import java.io.*; |
| | | //import java.util.Map; |
| | | // |
| | | //@RestController |
| | | //public class WordExportController { |
| | | // @ResponseBody |
| | | // @RequestMapping(value = "download") |
| | | // public int download(HttpServletResponse response, HttpServletRequest request)throws Exception { |
| | | // String content = "<h1>标题头</h1><h2>第二个标题</h2><a href=\"www.baidu.com\">百度搜索</a>"; |
| | | // StringBuffer sbf = new StringBuffer(); |
| | | // sbf.append("<html><body>"); |
| | | // sbf.append(content); |
| | | // sbf.append("</body></html"); |
| | | // exportWord(request,response,String.valueOf(sbf),"word1"); |
| | | // return 1; |
| | | // } |
| | | // |
| | | // |
| | | // /** |
| | | // * |
| | | // * @param request |
| | | // * @param response |
| | | // * @param content 富文本内容 |
| | | // * @param fileName 生成word名字 |
| | | // * @throws Exception |
| | | // */ |
| | | // public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception { |
| | | // byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。 |
| | | // ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中 |
| | | // POIFSFileSystem poifs = new POIFSFileSystem(); |
| | | // DirectoryEntry directory = poifs.getRoot(); |
| | | // DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //该步骤不可省略,否则会出现乱码。 |
| | | // //输出文件 |
| | | // request.setCharacterEncoding("utf-8"); |
| | | // response.setContentType("application/msword");//导出word格式 |
| | | // response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"),"iso8859-1") + ".doc"); |
| | | // ServletOutputStream ostream = response.getOutputStream(); |
| | | // poifs.writeFilesystem(ostream); |
| | | // bais.close(); |
| | | // ostream.close(); |
| | | // poifs.close(); |
| | | // } |
| | | // |
| | | // |
| | | // |
| | | // |
| | | //} |