获取Excel数据(或部分数据)并导出成txt文本格式
运行代码前先导入jxl架包,以下代码仅供参考:
测试excel文件(我要获取该excel的内容为省、县、乡、村、组和PH的值):
ExcelTest01类代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | // 读取Excel的类 import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class ExcelTest01 { public static void main(String args[]) { try { System.out.println( "begini" ); Workbook book = Workbook.getWorkbook( new File( "a.xls" )); // 获得第一个工作表对象 Sheet sheet = book.getSheet( 0 ); // 得到第一列第一行的单元格 try { File file = new File( "c:/IAI.txt" ); if (file.exists()) { file.delete(); } file.createNewFile(); BufferedWriter output = new BufferedWriter( new FileWriter(file)); for ( int i = 2 ; i < 4896 ; i++) { Cell cell1 = sheet.getCell( 23 , i); // PH Cell cell2 = sheet.getCell( 4 , i); // 省 Cell cell3 = sheet.getCell( 7 , i); // 县 Cell cell4 = sheet.getCell( 8 , i); // 乡 Cell cell5 = sheet.getCell( 9 , i); // 村 Cell cell6 = sheet.getCell( 10 , i); // 组 String ph = cell1.getContents(); String sheng = cell2.getContents(); String xian = cell3.getContents(); String xiang = cell4.getContents(); String cun = cell5.getContents(); String zu = cell6.getContents(); System.out.println( "第" + (i + 1 ) + "行" + sheng + xian + xiang + cun + zu+ "\t" +ph); output.write( "第" + (i + 1 ) + "行" + sheng + xian + xiang + cun + zu+ "\t" +ph+ "\n" ); output.newLine(); } output.close(); } catch (Exception ex) { System.out.println(ex); } book.close(); System.out.println( "end" ); } catch (Exception e) { System.out.println(e); } } } |
运行结果如下图所示:
生成txt文本内容如下:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix