/**
* 读取excel文件到Txt
*
* @author wild
*
*/
public class TestxlsOutTxt {
/**
* 读取xls文件内容
*
* @param file
* 想要读取的文件对象
* @return:Txt文件内容
*/
public static String xls2String(File file) {
String result = "";
try {
FileInputStream fis = new FileInputStream(file);
StringBuilder sb = new StringBuilder();
jxl.Workbook rwb = Workbook.getWorkbook(fis);
Sheet[] sheet = rwb.getSheets();
for (int i = 0; i < sheet.length; i++) {
Sheet rs = rwb.getSheet(i);
for (int j = 0; j < rs.getRows(); j++) {
Cell[] cells = rs.getRow(j);
for (int k = 0; k < cells.length; k++)
sb.append(cells[k].getContents());
}
}
fis.close();
result += sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
File file = new File("F:/测试的Excel.xls");
File fileTxt = new File("F:/测试的Excel.txt");
try {
fileTxt.createNewFile();
Writer out = null;
// 通过子类实例化,表示可以追加
out = new FileWriter(fileTxt, true);
// 写入数据
out.write(xls2String(file));
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Java 使用jxl读取Excel文件到Txt
最新推荐文章于 2021-02-13 09:26:30 发布