poi将File文件转为Array数组再转为Execl表格

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+"";
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值