TXT转换EXCEL

package Test;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;


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;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class TestJXL {
public static void main(String args[]) throws IOException, WriteException, WriteException
{
File txtfile = new File ("xxxxxxxxxxxxxxxxxxxx/rawdata.txt");
Scanner scan = new Scanner(txtfile);

File xlsfile = new File ("xxxxxxxxxxxxxxxxxxx/test.xls");
WritableWorkbook wwb = null;
wwb = Workbook.createWorkbook(xlsfile);
WritableSheet wSheet = wwb.createSheet("test",0);   // 建立一个sheet
 
WritableFont font1 = new WritableFont(WritableFont.ARIAL,10,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLUE);
WritableFont font2 = new WritableFont(WritableFont.ARIAL,8);
WritableCellFormat cf1 = new WritableCellFormat(font1);
WritableCellFormat cf2 = new WritableCellFormat(font2);//设定字体
 
int j =1;
while(scan.hasNext())
{
scan.nextLine();
String down = scan.nextLine();
String up = scan.nextLine();
String[] downtemp = down.split(" ");
String[] uptemp = up.split(" ");  //读取一个txt     txt 格式在附录
List<String> downlist = new ArrayList<String>();
List<String> uplist = new ArrayList<String>();
for(int i = 0;i<downtemp.length;i++)
{
downlist.add(downtemp[i]);
uplist.add(uptemp[i]);  
}
 
for(int i = 1;i<=downlist.size();i++)
{
Label l = new Label(i-1,0,String.valueOf(i));
wSheet.addCell(l);    //  加标签   addcell
}
 
Random r = new Random();
 
for(int i = 0;i<downlist.size();i++)
{
jxl.write.Number number1 = new jxl.write.Number(i, j,Integer.valueOf(downlist.get(i)),cf2);   
jxl.write.Number number2 = new jxl.write.Number(i, j+1,Integer.valueOf(uplist.get(i)),cf2);  //写入值  addcell
wSheet.addCell(number1);
wSheet.addCell(number2);
}
j+=2;
}
 
 
wwb.write();
wwb.close();   // 这两句很重要
}

}







txt的格式:

XXX
0 119 351 495 558 895 1030 1367 1496 1598
71 191 455 614 687 966 1142 1479 1631 1694
XXX
0 99 440 576 690 1144 1337 1625 1745 1849
81 201 544 713 769 1209 1433 1705 1881 1936
XXX
0 97 464 608 672 1032 1127 1504 1623 1703
80 183 567 728 794 1103 1239 1600 1751 1815
XXX
0 103 337 449 512 896 993 1736 1856 1967
79 184 424 559 640 976 1088 1824 1999 2079
XXX
0 96 407 503 583 999 1104 1487 1600 1679
63 176 495 639 720 1096 1191 1583 1744 1807
XXX
0 64 401 497 577 1009 1161 1497 1585 1656
74 169 514 632 705 1072 1280 1602 1737 1785

你可能感兴趣的:(java,txt,JXL,文本)