用java将excel数据导入txt
2019獨角獸企業重金招聘Python工程師標準>>>
package com.alibaba.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class WriteTxt {
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?String filepath = "e:\\demo.xls";
?? ??? ?try {
?? ??? ??? ?Workbook workbook = Workbook.getWorkbook(new File(filepath));
?? ??? ??? ?Sheet sheet = workbook.getSheet(0);
?? ??? ??? ?File fileSuccess = new File("success.txt");
?? ??? ??? ?File fileError = new File("error.txt");
?? ??? ??? ?FileWriter fwSuccess = new FileWriter(fileSuccess);
?? ??? ??? ?BufferedWriter bwSuccess = new BufferedWriter(fwSuccess);
?? ??? ??? ?FileWriter fwError = new FileWriter(fileError);
?? ??? ??? ?BufferedWriter bwError = new BufferedWriter(fwError);
?? ??? ??? ?// j為行數,getCell("列號","行號")
?? ??? ??? ?int j = sheet.getRows();
?? ??? ??? ?int y = sheet.getColumns();
?? ??? ??? ?Cell c = null;
?? ??? ??? ?for (int i = 0; i < j; i++) {
?? ??? ??? ??? ?String s = "";
?? ??? ??? ??? ?for (int x = 0; x < y; x++) {
?? ??? ??? ??? ??? ?c = sheet.getCell(x, i);
?? ??? ??? ??? ??? ?s += c.getContents() + " ? ?";
?? ??? ??? ??? ?}
?? ??? ??? ??? ?if (s.indexOf("&") != -1 || s.indexOf("@") != -1) {
?? ??? ??? ??? ??? ?bwSuccess.write(s);
?? ??? ??? ??? ??? ?bwSuccess.write(" ");
?? ??? ??? ??? ??? ?bwSuccess.flush();
?? ??? ??? ??? ??? ?// 讀取一行后換行
?? ??? ??? ??? ??? ?bwSuccess.newLine();
?? ??? ??? ??? ??? ?// 刷新
?? ??? ??? ??? ??? ?bwSuccess.flush();
?? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ?bwError.write(s);
?? ??? ??? ??? ??? ?bwError.write(" ");
?? ??? ??? ??? ??? ?bwError.flush();
?? ??? ??? ??? ??? ?bwError.newLine();
?? ??? ??? ??? ??? ?bwError.flush();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?System.out.println("寫入結束");
?? ??? ?} catch (BiffException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?? ?}
}
轉載于:https://my.oschina.net/u/3744350/blog/1604269
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的用java将excel数据导入txt的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Servlet重定向的过程(二)
- 下一篇: 设置 JAVA_HOME