Java实现读取Excel并按条件输出TXT
2018-01-19 13:26
351 查看
据我所知,java中能操作Excel文件的jar包有两个
Apache公司的一个API
微软的java Excel包
在这里我使用的是第二个,微软公司的java Excel包
这个包出现了一个为题,并不能顺利的输出规定的xls
会报错
在测试多次无果后,我使用了按格式输出txt作为代替
当然,至少在WPS中,可以直接导入txt作为Excel。影响不大
根据读取的xls文件不同,在代码中做一些相应修改就可以了
示例代码:
Apache公司的一个API
微软的java Excel包
在这里我使用的是第二个,微软公司的java Excel包
这个包出现了一个为题,并不能顺利的输出规定的xls
会报错
在测试多次无果后,我使用了按格式输出txt作为代替
当然,至少在WPS中,可以直接导入txt作为Excel。影响不大
根据读取的xls文件不同,在代码中做一些相应修改就可以了
示例代码:
package wangjiawen; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; public class Wangjiawen { public static void main(String [] args) { try { WritableWorkbook bookw = Workbook.createWorkbook(new File("2012(1).xls")); Workbook bookr = Workbook.getWorkbook(new File("绵阳市/2017.xls")); FileWriter fw = new FileWriter("绵阳市2017.txt"); PrintWriter printwrite = new PrintWriter(fw); for(int i=0; i<bookr.getNumberOfSheets(); i++) { Sheet sheetr = bookr.getSheet(i); @SuppressWarnings("unused") WritableSheet sheetw= bookw.createSheet(sheetr.getName(), i); int m = 0; int sunshineday = 0; int averageday = 0; int highestday = 0; int lowestday = 0; int humidityday = 0; int rainday = 0; String time = "2012-01"; float sunshine = 0; float average = 0; float highest = 0; float lowest = 0; float humidity = 0; float rain = 0; for(int j=1; j<sheetr.getRows(); j++) { @SuppressWarnings("unused") int n = 1; Cell[] cell = sheetr.getRow(j); if(cell.length > 10) { String newtime = cell[4].getContents(); if(newtime.length() > 7) { newtime = newtime.substring(0, 7); m++; if(newtime.equals(time)) { String cell5 = cell[5].getContents().toString(); if(cell5!="" && cell5!="#DIV/0!" && Float.parseFloat(cell5)<32000) { sunshine += Float.parseFloat(cell[5].getContents()); sunshineday++; } String cell6 = cell[6].getContents().toString(); if(cell6!="" && cell6!="#DIV/0!" && Float.parseFloat(cell6)<32000) { average += Float.parseFloat(cell[6].getContents()); averageday++; } String cell7 = cell[7].getContents().toString(); if(cell7!="" && cell7!="#DIV/0!" && Float.parseFloat(cell7)<32000) { highest += Float.parseFloat(cell[7].getContents()); highestday++; } String cell8 = cell[8].getContents().toString(); if(cell8!="" && cell8!="#DIV/0!" && Float.parseFloat(cell8)<32000) { lowest += Float.parseFloat(cell[8].getContents()); lowestday++; } String cell9 = cell[9].getContents().toString(); if(cell9!="" && cell9!="#DIV/0!" && Float.parseFloat(cell9)<32000) { humidity += Float.parseFloat(cell[9].getContents()); humidityday++; } String cell10 = cell[10].getContents().toString(); if(cell10!="" && cell10!="#DIV/0!" && Float.parseFloat(cell10)<32000) { rain += Float.parseFloat(cell[10].getContents()); rainday++; } }else { if(m>1) { System.out.printf("%s ", cell[0].getContents()); printwrite.printf("%s ", cell[0].getContents()); if(cell[1].getContents().length()>2) { System.out.printf("%s ", cell[1].getContents()); printwrite.printf("%s ", cell[1].getContents()); }else { System.out.printf("%s ", cell[1].getContents()); printwrite.printf("%s ", cell[1].getContents()); System.out.printf(""); printwrite.printf(""); } System.out.printf("%s ", cell[2].getContents()); System.out.printf(" %s ", cell[3].getContents()); System.out.printf(" %s ", time); System.out.printf(" %6.7f ", sunshine/sunshineday); System.out.printf(" %6.7f ", average/averageday); System.out.printf(" %6.7f ", highest/highestday); System.out.printf(" %6.7f ", lowest/lowest); System.out.printf(" %6.7f ", humidity/humidityday); System.out.printf(" %6.7f ", rain/rainday); System.out.printf(" %d ", m); System.out.println(""); printwrite.printf("%s ", cell[2].getContents()); printwrite.printf(" %s ", cell[3].getContents()); printwrite.printf(" %s ", time); printwrite.printf(" %6.7f ", sunshine/sunshineday); printwrite.printf(" %6.7f ", average/averageday); printwrite.printf(" %6.7f ", highest/highestday); printwrite.printf(" %6.7f ", lowest/lowestday); printwrite.printf(" %6.7f ", humidity/humidityday); printwrite.printf(" %6.7f ", rain/rainday); printwrite.printf(" %d ", m); printwrite.println(""); } m = 0; time = newtime; sunshineday = 0; averageday = 0; highestday = 0; lowestday = 0; humidityday = 0; rainday = 0; String cell5 = cell[5].getContents().toString(); if(cell5!="" && cell5!="#DIV/0!" && Float.parseFloat(cell5)<32000) { sunshine = Float.parseFloat(cell[5].getContents()); sunshineday++; }else { sunshine = 0; } String cell6 = cell[6].getContents().toString(); if(cell6!="" && cell6!="#DIV/0!" && Float.parseFloat(cell6)<32000) { average = Float.parseFloat(cell[6].getContents()); averageday++; }else { average = 0; } String cell7 = cell[7].getContents().toString(); if(cell7!="" && cell7!="#DIV/0!" && Float.parseFloat(cell7)<32000) { highest = Float.parseFloat(cell[7].getContents()); highestday++; }else { highest = 0; } String cell8 = cell[8].getContents().toString(); if(cell8!="" && cell8!="#DIV/0!" && Float.parseFloat(cell8)<32000) { lowest = Float.parseFloat(cell[8].getContents()); lowestday++; }else { lowest = 0; } String cell9 = cell[9].getContents().toString(); if(cell9!="" && cell9!="#DIV/0!" && Float.parseFloat(cell9)<32000) { humidity = Float.parseFloat(cell[9].getContents()); humidityday++; }else { humidity = 0; } String cell10 = cell[10].getContents().toString(); if(cell10!="" && cell10!="#DIV/0!" && Float.parseFloat(cell10)<32000) { rain = Float.parseFloat(cell[10].getContents()); rainday++; }else { rain = 0; } } } } // if(newtime.equals(time)) { // System.out.println(time); // time = newtime; // } if(j == sheetr.getRows()-1) { if(m>1) { System.out.printf("%s ", cell[0].getContents()); printwrite.printf("%s ", cell[0].getContents()); if(cell[1].getContents().length()>2) { System.out.printf("%s ", cell[1].getContents()); printwrite.printf("%s ", cell[1].getContents()); }else { System.out.printf("%s ", cell[1].getContents()); printwrite.printf("%s ", cell[1].getContents()); System.out.printf(""); printwrite.printf(""); } System.out.printf("%s ", cell[2].getContents()); System.out.printf(" %s ", cell[3].getContents()); System.out.printf(" %s ", time); System.out.printf(" %6.7f ", sunshine/sunshineday); System.out.printf(" %6.7f ", average/averageday); System.out.printf(" %6.7f ", highest/highestday); System.out.printf(" %6.7f ", lowest/lowest); System.out.printf(" %6.7f ", humidity/humidityday); System.out.printf(" %6.7f ", rain/rainday); System.out.printf(" %d ", m); System.out.println(""); printwrite.printf("%s ", cell[2].getContents()); printwrite.printf(" %s ", cell[3].getContents()); printwrite.printf(" %s ", time); printwrite.printf(" %6.7f ", sunshine/sunshineday); printwrite.printf(" %6.7f ", average/averageday); printwrite.printf(" %6.7f ", highest/highestday); printwrite.printf(" %6.7f ", lowest/lowestday); printwrite.printf(" %6.7f ", humidity/humidityday); printwrite.printf(" %6.7f ", rain/rainday); printwrite.printf(" %d ", m); printwrite.println(""); } } } } } catch (BiffException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
相关文章推荐
- 空间复杂度,实现从excel导出到txt文件中的java代码自动构建,逻辑条件不同实现则不同
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- Java实现读取键盘输入保存到txt文件,再统计并输出每个单词出现次数的方法
- 读取Excel列,转换为String输出(Java实现)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 【转】 Java 操作 Excel (读取Excel2007,Poi实现)
- java直接输出Excel 页面实现下载
- Java 操作 Excel (读取Excel2007,Poi实现)
- 知识总结-Java 操作 Excel (读取Excel2003 2007,Poi实现)
- java读取excel、txt 文件内容,传到、显示到另一个页面的文本框里面。
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- Java 操作 Excel (读取Excel2003 2007,Poi实现)
- EXCEL批量转化为TXT-EXCEL转化为TXT-java实现 Excel转txt
- Java实现txt文本的读取、写入、转移!
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java 操作 Excel (读取Excel2003 2007,Poi实现)
- 用java实现print screen,读取位图文件,将位图文件转化位jpeg格式并保存成文件输出!