我是靠谱客的博主 酷炫金针菇,最近开发中收集的这篇文章主要介绍实现excel读取成txt文件第一种jxl方式# 第二种poi方式读取excel生成txt文件文件下载工具类,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
第一种jxl方式
<!--excel读取1-->
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
package com.dxy.study_0628.util.excel;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import java.io.*;
public class ExceltoTxt_1124 {
public static void ToAnalysisExcel() {
// Unable to recognize OLE stream 不支持xlsx格式 支持xls格式
try {
Workbook workbook = null;
File Inputfile = new File("F:\xxxx\test\user\myInfo.xls");
File outputfile = new File("F:\xxxx\test\user\mtInfo.txt");
File outputfile2 = new File("F:\xxxx\test\user\mtInfo2.txt");
FileOutputStream fileOutputStream2 = new FileOutputStream(outputfile2);
BufferedOutputStream bw2 = new BufferedOutputStream(fileOutputStream2); //输出语句
FileInputStream fileInputStream = new FileInputStream(Inputfile);
workbook = Workbook.getWorkbook(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream(outputfile);
BufferedOutputStream bw = new BufferedOutputStream(fileOutputStream); //输出语句
Sheet readfirst = workbook.getSheet(0);
System.out.println(readfirst.getName());
int rows = readfirst.getRows();
int clomns = readfirst.getColumns();
System.out.println("row:" + rows);
System.out.println("clomns:" + clomns);
for (int i = 3; i < rows; i++) {
//循环得到每一行的单元格对象
Cell[] cells = readfirst.getRow(i);
//根据每一个单元格对象的到里面的值
String ce1 = cells[0].getContents();
String ce2 = cells[1].getContents();
String ce3 = cells[2].getContents();
String ce4 = cells[3].getContents();
//将得到的值放在一个我需要的格式的string对象中
String output = ce1 + "," + ce2 + "," + ce3 + "," + ce4 + "n";
System.out.print(output);
//write and flush到 我需要的文件中去,flush后才能成功
byte[] outputbyte = new String(output).getBytes();
bw.write(outputbyte);
System.out.println();
bw.flush();
}
Sheet readfirst1 = workbook.getSheet(1);
System.out.println(readfirst1.getName());
int rows1 = readfirst1.getRows();
int clomns1 = readfirst1.getColumns();
for (int i = 4; i < rows1; i++) {
//循环得到每一行的单元格对象
Cell[] cells = readfirst1.getRow(i);
//根据每一个单元格对象的到里面的值
String ce1 = cells[0].getContents();
String ce2 = cells[1].getContents();
String ce3 = cells[2].getContents();
String ce4 = cells[3].getContents();
//将得到的值放在一个我需要的格式的string对象中
String output = ce1 + "," + ce2 + "," + ce3 + "," + ce4 + "n";
System.out.print(output);
//write and flush到 我需要的文件中去,flush后才能成功
byte[] outputbyte = new String(output).getBytes();
bw2.write(outputbyte);
System.out.println();
bw2.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ExceltoTxt_1124.ToAnalysisExcel();
}
}
# 第二种poi方式
依赖
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
controller
/**
* 导出excel中的数据为txt,并下载txt文件
* @param response
* @throws Exception
*/
@RequestMapping(value = "/download_txt", method = RequestMethod.GET)
public void
download_txt(HttpServletResponse response) throws Exception {
FileDownloadUtil fileDownloadUtil = new FileDownloadUtil();
String s = ExcelPoi.poiRead();
fileDownloadUtil.download(response,s,"下载文件.txt");
}
读取excel生成txt文件
package com.dxy.study_0628.util.excel;
import com.github.fartherp.framework.poi.excel.read.ExcelRead;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
public class ExcelPoi {
public static void main(String[] args) throws IOException {
String s = ExcelPoi.poiRead();
System.out.println(s);
}
public static String poiRead() throws IOException {
//poi自带的excel读取类
ExcelRead excelRead = new ExcelRead();
//读取excel文件
//XSSFWorkbook wb = new XSSFWorkbook("F:\xxxx\test\user\excel.xls");
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream("F:\xxxx\test\user\excel.xls"));
//输出流,用于导出excel里面的内容
String outFile = "F:\xxxx\test\user\poi.txt";
FileOutputStream fileOutputStream = new FileOutputStream(new File(outFile));
BufferedOutputStream bw = new BufferedOutputStream(fileOutputStream);
//获取第一个sheel
//XSSFSheet sheetAt = wb.getSheetAt(0);
HSSFSheet sheetAt = wb.getSheetAt(0);
//获取最后一格
int lastRowNum = sheetAt.getLastRowNum();
System.out.println("lastRowNum:"+lastRowNum);
//获取名字
String sheetName = sheetAt.getSheetName();
System.out.println("sheetName:"+sheetName);
//循环遍历第一个sheet
for (int i=0;i<lastRowNum;i++){
//遍历每一行表格
//XSSFRow row = sheetAt.getRow(i);
HSSFRow row = sheetAt.getRow(i);
HSSFCell cell = row.getCell(0);
HSSFCell cell1 = row.getCell(1);
HSSFCell cell2 = row.getCell(2);
HSSFCell cell3 = row.getCell(3);
String output = cell + "," + cell1 + "," + cell2 + "," + cell3 + "n";
System.out.println(output);
byte[] outputbyte = new String(output).getBytes();
bw.write(outputbyte);
bw.flush();
}
return outFile;
}
}
文件下载工具类
package com.dxy.study_0628.util;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class FileDownloadUtil {
public void downloadLocal(HttpServletResponse response) throws FileNotFoundException, UnsupportedEncodingException {
// 下载本地文件
//String fileName = "图片.gif".toString(); // 文件的默认保存名
// String fileName = new String("图片.gif".getBytes(),"ISO-8859-1"); // 文件的默认保存名
String fileName = URLEncoder.encode("图片.gif","UTF-8");
// 读到流中
InputStream inStream = new FileInputStream("f:/abc.gif");// 文件的存放路径
// 设置输出的格式
// 支持中文名称文件,需要对header进行单独设置,不然下载的文件名会出现乱码或者无法显示的情况
// String downloadFileName = new String(fileName .getBytes(), "ISO-8859-1");
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void downloadNet(HttpServletResponse response) throws IOException {
// 下载网络文件
int bytesum = 0;
int byteread = 0;
URL url = new URL("http://localhost:8080/zzz.zip");
try {
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs = new FileOutputStream("f:/zz3.zip");
byte[] buffer = new byte[1204];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 支持在线打开的方式
* @param filePath
* @param response
* @param isOnLine
* @throws Exception
*/
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" +
URLEncoder.encode(f.getName(),"UTF-8"));
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" +
URLEncoder.encode(f.getName(),"UTF-8"));
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
br.close();
out.close();
}
/**
* 传入文件地址下载
* @param response
* @param filePath 文件地址
* @param newName
新名字
* @throws FileNotFoundException
* @throws UnsupportedEncodingException
*/
public void download(HttpServletResponse response,String filePath,String newName) throws FileNotFoundException, UnsupportedEncodingException {
// 下载本地文件
//String fileName = "图片.gif".toString(); // 文件的默认保存名
// String fileName = new String("图片.gif".getBytes(),"ISO-8859-1"); // 文件的默认保存名
String fileName = URLEncoder.encode(newName,"UTF-8");
// 读到流中
InputStream inStream = new FileInputStream(filePath);// 文件的存放路径
// 设置输出的格式
// 支持中文名称文件,需要对header进行单独设置,不然下载的文件名会出现乱码或者无法显示的情况
// String downloadFileName = new String(fileName .getBytes(), "ISO-8859-1");
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition", "attachment; filename="" + fileName + """);
// 循环取出流中的数据
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
FileDownloadUtil fileDownloadUtil = new FileDownloadUtil();
HttpServletResponse httpServletResponse = null;
fileDownloadUtil.downloadNet(httpServletResponse);
}
}
最后
以上就是酷炫金针菇为你收集整理的实现excel读取成txt文件第一种jxl方式# 第二种poi方式读取excel生成txt文件文件下载工具类的全部内容,希望文章能够帮你解决实现excel读取成txt文件第一种jxl方式# 第二种poi方式读取excel生成txt文件文件下载工具类所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复