1. 程式人生 > >java 從Excel 輸出和輸入

java 從Excel 輸出和輸入

sts creat 信息 ddc 實現 pri all tro rac

本文實現了使用java 從數據庫中獲得對象,並存入集合中,

然後輸出到Excel,並設置樣式

 1 package com.webwork;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.List;
 6 
 7 import org.jdom.output.Format;
 8 
 9 import jxl.Workbook;
10 import jxl.write.Label;
11 import jxl.write.WritableCellFormat;
12 import jxl.write.WritableFont;
13 import jxl.write.WritableSheet; 14 import jxl.write.WritableWorkbook; 15 import jxl.write.WriteException; 16 import jxl.write.biff.RowsExceededException; 17 18 public class Jxls { 19 public static void main(String[] args) { 20 try { 21 new Jxls().writeExcel(); 22 } catch
(RowsExceededException e) { 23 e.printStackTrace(); 24 } catch (WriteException e) { 25 e.printStackTrace(); 26 } catch (IOException e) { 27 e.printStackTrace(); 28 } 29 } 30 public void writeExcel() throws RowsExceededException, WriteException, IOException{
31 new Jdbc().addList(); 32 List< Student> stuLists = Jdbc.getStuList(); 33 WritableWorkbook book =null; 34 35 book = Workbook.createWorkbook(new File("e:/io/stuInfo.xls")); 36 37 WritableSheet sheet = book.createSheet("studentInfo", 0); 38 39 /* 40 * format設置樣式 41 */ 42 //設置字體 43 WritableFont font1 = new WritableFont(WritableFont.ARIAL,18,WritableFont.BOLD); 44 WritableFont font2 = new WritableFont(WritableFont.ARIAL, 13, WritableFont.BOLD); 45 //格式化單元格 46 WritableCellFormat format1 = new WritableCellFormat(font1); 47 WritableCellFormat format2 = new WritableCellFormat(font2); 48 WritableCellFormat format3 = new WritableCellFormat(); 49 //設置樣式 50 format1.setAlignment(jxl.format.Alignment.CENTRE);//水平對齊方式 51 format1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE); //垂直對齊方式 52 format1.setBackground(jxl.format.Colour.LIGHT_BLUE);//設置背景顏色 53 format1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);//設置邊框 54 55 56 //合並單元格第一行 57 sheet.mergeCells(0, 0, 3, 0); 58 Label label = new Label(0, 0, "yc95全班信息表",format1); 59 sheet.addCell(label); 60 61 //設置每列小標題 62 String [] str = {"id","name","sex","age"}; 63 for (int i = 0; i < str.length; i++) { 64 sheet.addCell(new Label(i, 1, str[i])); 65 sheet.setColumnView(i, 20);//設置列寬 66 } 67 68 for (int i = 2; i < stuLists.size(); i++) { 69 Student s = stuLists.get(i); 70 //System.out.println(s); 71 Label label1 = new Label(0,i ,s.getId()); 72 Label label2 = new Label(1,i, s.getName()); 73 Label label3 = new Label(2,i, s.getSex()); 74 Label label4 = new Label(3,i, s.getAge()); 75 sheet.addCell(label1); 76 sheet.addCell(label2); 77 sheet.addCell(label3); 78 sheet.addCell(label4); 79 } 80 book.write(); 81 book.close(); 82 System.out.println("插入excel數據成功!!!"); 83 } 84 }

下面是excel中得到的內容

技術分享

輸出到Excel後,相應的就是從Excel中去取數據

 1 package com.webwork;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 import java.util.List;
 6 
 7 import jxl.Cell;
 8 import jxl.Sheet;
 9 import jxl.Workbook;
10 import jxl.read.biff.BiffException;
11 
12 public class ReadExcel {
13     public static void main(String[] args) {
14         List<Student> stuLists = Jdbc.getStuList();
15         Workbook book = null;
16         try {
17             book  = Workbook.getWorkbook(new File("e:/io/stuInfo.xls"));
18         } catch (BiffException e) {
19             e.printStackTrace();
20         } catch (IOException e) {
21             e.printStackTrace();
22         }
23         Sheet[] sheets = book.getSheets();
24         for (int i = 0; i < sheets.length; i++) {
25             Sheet sheet = sheets[i];
26             int rows=  sheet.getRows();
27             for (int j = 1; j < rows; j++) {
28                 Cell[] cells= sheet.getRow(j);
29                 for (int k = 0; k < cells.length; k++) {
30                     Cell cell = cells[k];
31                     String id = cell.getContents();
32                     System.out.print(id+"\t");
33                 }
34                 
35                 System.out.println();
36             }
37             
38         }
39         
40         
41         
42     }
43 }

輸出的結果如下:

技術分享

java 從Excel 輸出和輸入