简单的示范代码如下:
package xls.test;
import java.io.File;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class XlsExample {
public static void main(String[] args) {
try {
WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));
WritableSheet sheet = book.createSheet("sheet0", 0);
int col = 0;
int row = 0;
sheet.addCell(new Label(col++, row, "语文"));
sheet.addCell(new Label(col++, row, "数学"));
sheet.addCell(new Label(col++, row, "英语"));
sheet.addCell(new Label(col++, row, "物理"));
sheet.addCell(new Label(col++, row, "化学"));
sheet.addCell(new Label(col++, row, "生物"));
for(row = 1;row < 10;){
col = 0;
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
sheet.addCell(new Number(col++, row, (int)(Math.random()*100)));
row++;
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果如下:
语文数学英语物理化学生物
15315231276
356759452976
3091049389
882763392018
117828126568
308943285384
215540977818
563174104186
13402754268
这些代码还是可以派上用场的,一个朋友在做日志分析,要将几十个像下面格式日志信息导入到excel中:
2013-10-20 日志记录
========================================
---------- 00:00:00 ----------
机器人已经收到命令
---------- 00:00:04 ----------
收到机器人起始抓表完成!
---------- 00:00:11 ----------
收到机器人命令完成!
---------- 00:00:12 ----------
机器人执行从1到1取表位抓到上表位命令成功!
导出的格式如下所示:
xxxx 00:00:01 00:00:02
xxxx 00:00:02 00:00:23 10:00:00
下面是示例代码,以后有类似的可以拿来参考一下:
1 importjava.io.BufferedReader;2 importjava.io.File;3 importjava.io.FileInputStream;4 importjava.io.IOException;5 importjava.io.InputStreamReader;6 importjava.util.Map.Entry;7 importjava.util.regex.Matcher;8 importjava.util.regex.Pattern;9 importjava.util.ArrayList;10 importjava.util.Iterator;11 importjava.util.Map;12 importjava.util.List;13 importjava.util.HashMap;14 importjava.util.Set;15
16 importjxl.Workbook;17 importjxl.write.Label;18 importjxl.write.WritableSheet;19 importjxl.write.WritableWorkbook;20 importjxl.write.WriteException;21 importjxl.write.biff.RowsExceededException;22 public classLogProcess {23 String regx="\\-+ \\d{2}:\\d{2}:\\d{2} \\-+";24 Pattern p =Pattern.compile(regx);25 public Map> map = new HashMap>();26 public void processLog(BufferedReader br) throwsIOException{27 String temp1=null;28 temp1=br.readLine();29 while(temp1 != null){30 Matcher m =p.matcher(temp1);31 boolean match =m.matches();32 if(match)33 break;34 temp1=br.readLine();35 }36 while(temp1!=null){37 Matcher m =p.matcher(temp1);38 boolean match =m.matches();39 if(!match){40 ;//System.out.println("输入格式不正确!");
41 }else{42 ;//System.out.println("输入格式正确!"+ temp1);
43 }44 String temp2=br.readLine();45 if( temp2 != null && !temp2.isEmpty()){46 if(map.containsKey(temp2)){47 temp1 = temp1.replaceAll("-", "").trim();48 map.get(temp2).add(temp1);49
50 }else{51 List tmpList = new ArrayList();52 temp1 = temp1.replaceAll("-", "").trim();53 tmpList.add(temp1);54 map.put(temp2, tmpList);55 }56 }57 temp1 =br.readLine();58 temp1 =br.readLine();59 }60 }61 public void writeToExcel(String fileName,String path) throwsIOException, RowsExceededException, WriteException{62 String xlsName = path + fileName+".xls";63 WritableWorkbook book = Workbook.createWorkbook(newFile(xlsName));64 WritableSheet sheet = book.createSheet("sheet0", 0);65 int col = 0;66 int row = 0;67 Set>> set =map.entrySet();68 Iterator>> it =set.iterator();69 while(it.hasNext()){70 Map.Entry> entry =it.next();71 String key =entry.getKey();72 sheet.addCell(newLabel(col, row, key));73 col++;74 List list =entry.getValue();75 StringBuffer bf = newStringBuffer();76 System.out.println(key + ":" +bf.toString());77 }78 Set>> set2 =map.entrySet();79 Iterator>> it2 =set2.iterator();80 col = 0;81 row = 1;82 while(it2.hasNext()){83 int newrow = 1;84 Map.Entry> entry =it2.next();85 String key =entry.getKey();86 List list =entry.getValue();87 StringBuffer bf = newStringBuffer();88 for(int i = 0; i < list.size(); i++){89 bf.append(list.get(i));90 bf.append("\t");91 sheet.addCell(new Label(col, newrow++,list.get(i)));92 }93 col++;94 System.out.println(key + ":" +bf.toString());95 }96 book.write();97 book.close();98 }99 public void writeToExcel2(String fileName,String path) throwsIOException, RowsExceededException, WriteException{100 String xlsName = path + fileName+".xls";101 File file = newFile(xlsName);102 if(file.exists())103 file.delete();104 WritableWorkbook book =Workbook.createWorkbook(file);105 WritableSheet sheet = book.createSheet("sheet0", 0);106 int col = 0;107 int row = 0;108 Set>> set =map.entrySet();109 Iterator>> it =set.iterator();110 while(it.hasNext()){111 Map.Entry> entry =it.next();112 String key =entry.getKey();113 sheet.addCell(newLabel(col, row, key));114 row++;115 List list =entry.getValue();116 StringBuffer bf = newStringBuffer();117 System.out.println(key + ":" +bf.toString());118 }119 Set>> set2 =map.entrySet();120 Iterator>> it2 =set2.iterator();121 col = 1;122 row = 0;123 while(it2.hasNext()){124 int newcol = 1;125 Map.Entry> entry =it2.next();126 String key =entry.getKey();127 List list =entry.getValue();128 StringBuffer bf = newStringBuffer();129 for(int i = 0; i < list.size(); i++){130 bf.append(list.get(i));131 bf.append("\t");132 sheet.addCell(new Label(newcol++, row,list.get(i)));133 }134 row++;135 //System.out.println(key + ":" + bf.toString());
136 }137 book.write();138 book.close();139 }140 public static voidmain(String[] args) {141 try{142 String path = "E:\\workspace\\log\\";143 String outputPath = "E:\\workspace\\excel\\";144 File file=newFile(path);145 File files[] =file.listFiles();146 for(int i = 0; i < files.length; i++){147 String fileName =files[i].getName();148 System.out.println(fileName);149 InputStreamReader insReader = new InputStreamReader(new FileInputStream(files[i]),"utf-8");150 BufferedReader br = newBufferedReader(insReader);151 LogProcess logProcess = newLogProcess();152 logProcess.processLog(br);153 try{154 logProcess.writeToExcel2(fileName,outputPath);155 }catch(Exception ex){156 System.out.println("write excel exception.");157 logProcess.writeToExcel(fileName,outputPath);158 }159 }160 } catch(Exception e) {161 e.printStackTrace();162 }163 }164 }
View Code