Java實現讀取Excel並按條件輸出TXT
阿新 • • 發佈:2019-02-19
據我所知,java中能操作Excel檔案的jar包有兩個
- 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();
}
}
}