您的位置:首页 > 编程语言 > Java开发

Java 使用jxl读取Excel文件到Txt

2017-08-03 17:08 281 查看
/**
* 读取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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  excel