import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class FileToExecl {
public static void main(String[] args) throws Exception {
File file = new File("C:\\Users\\Administrator\\Desktop\\407考场(13点场)");
File[] array = file.listFiles();
String str[][] = new String[4][100];
//System.out.println(array[0].getName());
int i = 0 ;
for(File f : array) {
System.out.println(f.getName());
File[] fzi = f.listFiles();
if(i != 0) {
for(int j = 0 ; j< fzi.length ; j++) {
System.out.println(fzi[j].getName());
str[2][j] = fzi[j].getName().substring(0, 8).trim();
int indexI = fzi[j].getName().trim().indexOf(".");
str[3][j] = fzi[j].getName().trim().substring(8, indexI).trim();
System.out.println(str[3][j]);
}
} else {
for(int j = 0 ; j< fzi.length ; j++) {
System.out.println(fzi[j].getName());
str[0][j] = fzi[j].getName().substring(0, 8).trim();
int indexI = fzi[j].getName().trim().indexOf(".");
str[1][j] = fzi[j].getName().trim().substring(8, indexI).trim();
System.out.println(str[1][j]);
}
}
i++;
}
//array转为Execl
ArrayToExecl(null, str);
}
public static void ArrayToExecl(String ExeclName,String [][]array) throws Exception {
//转为EXECL
WritableWorkbook wwb = Workbook.createWorkbook(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\18级考试成绩.xls"));
WritableSheet ws = wwb.createSheet("sheep", 0);
Label label;
//label里面的样式
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfc);
//首列数据
label = new Label(0, 0, "名次", wcfFC);
ws.addCell(label);
label = new Label(1, 0, "学号", wcfFC);
ws.addCell(label);
label = new Label(2, 0, "姓名", wcfFC);
ws.addCell(label);
label = new Label(3, 0, "试卷类型", wcfFC);
ws.addCell(label);
label = new Label(4, 0, "成绩", wcfFC);
ws.addCell(label);
int index = 0;
//数据
for (int i = 0; i < array[0].length; i++) {
if(array[0][i] == null) {
break;
}
String indexI = ++index + "";
label = new Label(0, i+1, indexI);
ws.addCell(label);
label = new Label(1, i+1, array[0][i]);
ws.addCell(label);
label = new Label(2, i+1, array[1][i]);
ws.addCell(label);
label = new Label(3, i+1, "A卷");
ws.addCell(label);
label = new Label(4, i+1, RandomNum.getRandomNum());
ws.addCell(label);
}
int lenthA = array[0].length;
for (int i = 0; i < array[2].length; i++) {
if(array[2][i] == null) {
break;
}
String indexI = ++index + "";
label = new Label(0, i+lenthA+1, indexI);
ws.addCell(label);
label = new Label(1, i+lenthA+1, array[2][i]);
ws.addCell(label);
label = new Label(2, i+lenthA+1, array[3][i]);
ws.addCell(label);
label = new Label(3, i+lenthA+1, "B卷");
ws.addCell(label);
label = new Label(4, i+lenthA+1, RandomNum.getRandomNum());
ws.addCell(label);
}
wwb.write();
wwb.close();
}
}
public class RandomNum {
public static String getRandomNum() {
int str = (int) (Math.random()*20+69);
return str+"";
}
}
poi将File文件转为Array数组再转为Execl表格
最新推荐文章于 2024-07-03 16:33:10 发布