据我所知,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();
}
}
}