1. 导入poi的jar包
(1)下载poi的jar包
(2)将相关的pio的jar包导入项目中
2. java代码如下所示:(将"测试.xlsx"中的第一列数据取出来存入"测试.txt"中)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class OutTxt {
public static void main(String args[]) throws IOException {
ArrayList<String> columnList = new ArrayList<String>();
File file = new File("D:\\测试用例/测试.xlsx");
FileWriter filewriter=new FileWriter("D:\\测试用例/测试.txt");
try {
FileInputStream in = new FileInputStream(file);
XSSFWorkbook wb = new XSSFWorkbook(in);
Sheet sheet = wb.getSheetAt(0); //取得“测试.xlsx”中的第一个表单
int firstRowNum = sheet.getFirstRowNum();
int lastRowNum = sheet.getLastRowNum();
Row row = null;
Cell cell_a = null;
for (int i = firstRowNum+1; i <= lastRowNum; i++) {
row = sheet.getRow(i); //取得第i行 (从第二行开始取,因为第一行是表头)
cell_a = row.getCell(0); //取得i行的第一列
String cellValue = cell_a.getStringCellValue().trim();
//System.out.println(cellValue);
filewriter.write(cellValue+"\r\n"); //将取出的.xlsx表中的数据写入到txt文件中
columnList.add(cellValue);
}
filewriter.flush();
filewriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
其中:测试.xlsx
导出成功后的测试.txt