• 获取Excel数据(或部分数据)并导出成txt文本格式


    运行代码前先导入jxl架包,以下代码仅供参考:

    测试excel文件(我要获取该excel的内容为省、县、乡、村、组和PH的值):

    ExcelTest01类代码如下:

    // 读取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+"	"+ph);
    					output.write("第" + (i + 1) + "行" + sheng + xian + xiang
    							+ cun + zu+"	"+ph+"
    ");
    					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文本内容如下:

  • 相关阅读:
    轮播插件unsilder 源码解析(二)
    轮播插件unsilder 源码解析(一)---源码解析
    轮播插件unsilder 源码解析(一)---使用
    jquery插件扩展的学习
    基于canvas的陈列订货的分析
    grunt安装和使用教程
    2016订货会项目总结2
    2016工作项目完成总结
    不使用递归法求值
    easy bootstrap模板
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3837491.html
Copyright © 2020-2023  润新知