实现JAVA导出Excel、TXT、批量导出ZIP文件。
区别:
读写单位:顾名思义,字节流以字节(byte)为读写单位,而字符流以字符为读写单位,根据码表映射字符,一次可能读入多个字符。
处理对象:字节流可以处理所有类型的数据(包括图片等),而字符流只能处理字符类型的纯文本数据。
字节流:一次写入或读出8位二进制。
字符流:一次写入或读出至少8位二进制。不同的字符所占的字节是不同的。
ASCII码:
一个英文字母(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间。一个二进制数字序列,在计算机中作为一个数字单元,一般为8位二进制数,换算为十进制。最小值0,最大值255。如一个ASCII码就是一个字节。
UTF-8编码:
一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。
Unicode编码:
一个英文等于两个字节,一个中文(含繁体)等于两个字节。
/* 导出txt文件
* @author
* @param response
* @param text 要导出的字符串
* @return
*/
public void exportTxt(HttpServletResponse response,String text){
response.setCharacterEncoding("utf-8");
//设置响应的内容类型
response.setContentType("text/plain");
//设置文件的名称和格式
response.addHeader("Content-Disposition","attachment;filename="
+ new String(("文件中文名称").getBytes("gb2312"),"iso8859-1")//设置名称格式,没有这个中文名称无法显示
+ ".txt");
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
try {
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(text.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
//LOGGER.error("导出文件文件出错:{}",e);
} finally {try {
buff.close();
outStr.close();
} catch (Exception e) {
//LOGGER.error("关闭流对象出错 e:{}",e);
}
}
}
【java页面导出TXT】
SSM框架下导出TXT,参考【SSM框架, 导出TXT】
未使用
) 构造需基于outputStream
public void readFileWithBufferedStream(String srcPath, int size) {
InputStream inputStream = null;
BufferedInputStream bufferedInputStream = null;
StringBuffer sb = new StringBuffer();
try {
inputStream = new FileInputStream(srcPath);
bufferedInputStream = new BufferedInputStream(inputStream);
byte[] byteArr = new byte[size];
int len = 0;
while ((len = bufferedInputStream.read(byteArr)) != -1) {
sb.append(new String(byteArr), 0, len);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
构造需基于outputStream
public void writeFileWithBufferedStream(String content, String destPath) {
OutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
// 下面的语句中,如果destPath中的目录不存在,不会自动创建目录,因此,建议使用下面的代码
// outputStream = new FileOutputStream(destPath);
// 前提是输出的路径是"/"而不是"\\"
File tmp = new File(destPath.substring(0, destPath.lastIndexOf("/")));
if(!tmp.exists()){
tmp.mkdirs();
}
outputStream = new FileOutputStream(destPath);
bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] byteArr = content.getBytes();
bufferedOutputStream.write(byteArr);
bufferedOutputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//后面用到
public static void exportExcelFromContent(HttpServletResponse response, String data, String savefileName)
throws Exception{
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] b =base64Decoder.decodeBuffer(data); //data为经过base64加密的字符,所以解码为字节流
InputStream inputStream = new ByteArrayInputStream(b);
Workbook wb = null;
String headStr = "attachment; filename=" + new String((savefileName).getBytes("gb2312"),"iso8859-1");
//制表平台只支持xlsx
headStr +='.xlsx';
wb= new XSSFWorkbook(inputStream);
//输出流
OutputStream out = null;
try{
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
out = response.getOutputStream();
wb.write(out);
}
catch (IOException e){
logger.error("导出Excel文件出错!", e);
} finally{
if(out != null){
out.close();
}
}
}
//压缩 文件
public static void doCompress(ZipOutputStream zos, File file, String baseDir) throws Exception {
if (file.isDirectory()) {
// 递归循环,只压缩其中所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
doCompress(zos, files[i], baseDir);
}
} else {
// 进行文件压缩的操作
byte[] buff = new byte[DEFAULT_BUFF_SIZE];
InputStream in = new FileInputStream(file);
zos.putNextEntry(new ZipEntry(baseDir + File.separator + file.getName()));
int len;
while ((len = in.read(buff,0 ,DEFAULT_BUFF_SIZE)) != -1) {
zos.write(buff, 0, len);
}
in.close();
}
}
@RequestMapping(value ="/exportReport.json")
public void exportFile(HttpServletResponse response, String action , String filenameList )throws Exception{
try {
JSONArray jsonArray = JSONArray.fromObject(filenameList);
List<Map<String, String>> resultMap = JSONArray.toList(jsonArray, new HashMap<String, String>(), new JsonConfig());
if(resultMap.size() == 1){
//单个导出
String reportName = resultMap.get(0).get("reportName");
String coop_branch_no = String.valueOf(resultMap.get(0).get("coop_branch_no"));
String reportId = StringUtils.equals("0",coop_branch_no)? String.valueOf(resultMap.get(0).get("report_id")) : String.valueOf(resultMap.get(0).get("report_id"))+"[" + coop_branch_no + "]" ;
String branchNo = String.valueOf(resultMap.get(0).get("branch_no"));
//报送导出页面 period_date=0L
String reportContent = bizReportService.getReportContent(reportId, branchNo, coop_branch_no,0L); //获取EXcel文件内容,reportContent为base64编码后的字符
FileUtils.exportExcelFromContent(response, reportContent, StringUtils.equals("0",coop_branch_no)? reportName:reportName+"[" + coop_branch_no + "]");
}else if(resultMap.size() > 1){
//批量导出
String reportName,coop_branch_no,branchNo,reportId,filePath = "";
response.reset();
//用于输出新生成文件名 keys: filePath fileName
List<Map<String, String>> filePaths = new ArrayList<>();
for(Map<String,String> file : resultMap){
Map<String,String> zipFileOutput = new HashMap<>();
if(file != null){
//组合成完整的文件路径
reportName = String.valueOf(file.get("reportName"));
String newFileName = StringUtils.equals("0",coop_branch_no)? reportName + ".xlsx" :reportName+"[" + coop_branch_no + "]" + ".xlsx";
zipFileOutput.put("reportId",reportId);
zipFileOutput.put("branchNo",branchNo);
zipFileOutput.put("coop_branch_no",coop_branch_no);
zipFileOutput.put("fileName",newFileName);
filePaths.add(zipFileOutput);
}
}
//创建压缩文件需要的空的zip包
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String zipName = dateFormat.format(date) + ".zip";
String zipFilePath = filePath+zipName;
//压缩文件
File zip = new File(zipFilePath);
if (!zip.exists()){
zip.createNewFile();
}
//创建zip文件输出流
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
this.zipFileFromContent(filePaths, zos);
zos.close();
//设置编码字符
response.setContentType("text/html; charset=UTF-8");
//设置内容类型为下载类型
response.setContentType("application/octet-stream");
//设置下载的压缩文件名称
response.setHeader("Content-disposition", "attachment;filename="+zipName);
//创建页面返回方式为输出流,会自动弹出下载框
OutputStream out = response.getOutputStream();
//将打包后的文件使用缓冲流输出;这种方式与直接 outputStream输出文件内容的字节流,不太一样;这里先读后写
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
byte[] buff = new byte[bis.available()];
bis.read(buff);
bis.close();
//输出数据文件
out.write(buff);
//释放缓存
out.flush();
//关闭输出流
out.close();
new File(zipFilePath).delete();
}
} catch (Exception e) {
logger.error("导出excel文件出错:"+e.getMessage());
}
}
//私有方法
private String zipFileFromContent(List<Map<String, String>> filePaths, ZipOutputStream zos) throws IOException {
//循环读取集合,获取每一个报表信息以获取报表内容
for(Map<String,String> filePath : filePaths){
String reportContent = bizReportService.getReportContent(filePath.get("reportId"), filePath.get("branchNo"),
filePath.get("coop_branch_no"),0L);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] b =base64Decoder.decodeBuffer(reportContent);
InputStream in = new ByteArrayInputStream(b);
//创建输入流读取文件
BufferedInputStream bis = new BufferedInputStream(in) ;
//将文件写入zip内,即将文件进行打包
zos.putNextEntry(new ZipEntry(filePath.get("fileName")));
//写入文件的方法,同上
int size = 0;
//设置读取数据缓存大小
byte[] buffer = new byte[1024];
while ((size = bis.read(buffer)) > 0) {
zos.write(buffer, 0, size);
}
//关闭输入输出流
zos.closeEntry();
bis.close();
}
return null;
}
@RequestMapping(value ="/exportTXT.json")
public void exportTXT(String txtnameList, HttpServletResponse resp)throws Exception {
// 清空输出流
resp.reset();
try{
JSONArray jsonArray = JSONArray.fromObject(txtnameList);
List<Map<String, String>> resultMap = JSONArray.toList(jsonArray, new HashMap<String, String>(), new JsonConfig());
if(resultMap.size() == 1){
String reportId = resultMap.get(0).get("report_id");
String branchNo = resultMap.get(0).get("branch_no");
String reportName = resultMap.get(0).get("reportName");
String coopBranchNo = resultMap.get(0).get("coop_branch_no");
String periodDate = "0";
String reportData = uraReportExportService.exportTXTFile(reportId, branchNo, coopBranchNo, periodDate);
if ("fail".equals(reportData)) {
logger.error("获取TXT内容失败");
} else {
resp.setCharacterEncoding("UTF-8");
// 设定输出文件头
resp.setHeader("Content-disposition", "attachment; filename=" + new String((reportName).getBytes("gb2312"),"iso8859-1") + ".TXT");
// 定义输出类型
resp.setContentType("text/plain");
//输出流
BufferedOutputStream buff = null;
OutputStream out = resp.getOutputStream();
buff = new BufferedOutputStream(out);
buff.write(reportData.getBytes("utf-8"));
buff.flush();
buff.close();
logger.info("TXT文件导出成功");
}
} else if(resultMap.size() > 1){
//批量导出TXT
String reportName,coopBranchNo,branchNo,reportId,filePath = "";
resp.reset();
List<Map<String, String>> filePaths = new ArrayList<>();
for(Map<String,String> file : resultMap){
Map<String,String> zipFileOutput = new HashMap<>();
if(file != null){
//组合成完整的文件路径
reportName = String.valueOf(file.get("reportName"));
String newFileName = StringUtils.equals("0",coopBranchNo)? reportName + ".TXT" :reportName+"[" + coopBranchNo + "]" + ".TXT";
zipFileOutput.put("reportId",reportId);
zipFileOutput.put("branchNo",branchNo);
zipFileOutput.put("coop_branch_no",coopBranchNo);
zipFileOutput.put("fileName",newFileName);
filePaths.add(zipFileOutput);
}
}
//创建压缩文件需要的空的zip包
String zipBasePath= filePath ;
Date date = new Date();
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String zipName = dateFormat.format(date) + ".zip";
String zipFilePath = filePath+zipName;
//压缩文件
File zip = new File(zipFilePath);
if (!zip.exists()){
zip.createNewFile();
}
//创建zip文件输出流
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zip));
this.zipFileFromTXTContent(filePaths, zos);
zos.close();
//设置编码字符
resp.setContentType("text/html; charset=UTF-8");
//设置内容类型为下载类型
resp.setContentType("application/octet-stream");
//设置下载的压缩文件名称
resp.setHeader("Content-disposition", "attachment;filename="+zipName);
//创建页面返回方式为输出流,会自动弹出下载框
OutputStream out = resp.getOutputStream();
//将打包后的文件使用缓冲流输出
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(zipFilePath));
byte[] buff = new byte[bis.available()];
bis.read(buff);
bis.close();
//输出数据文件
out.write(buff);
//释放缓存
out.flush();
//关闭输出流
out.close();
new File(zipFilePath).delete();
}
} catch (Exception e){
logger.error("TXT导出失败",e.toString());
}
}