java 從Excel 輸出和輸入

來源:互聯網
上載者:User

標籤:水平對齊   font   內容   輸出   ack   creat   put   格式   資料   

本文實現了使用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 輸出和輸入

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.