zoukankan      html  css  js  c++  java
  • java 从Excel 输出和输入

    本文实现了使用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+"	");
    33                 }
    34                 
    35                 System.out.println();
    36             }
    37             
    38         }
    39         
    40         
    41         
    42     }
    43 }

    输出的结果如下:

  • 相关阅读:
    C#执行sql文件
    "C:Program FilesInternet Exploreriexplore.exe" -extoff 无加载项启动IE 浏览器打开时全屏模式
    sql server备份与还原 sql语句
    触摸屏\串口服务器\串口
    USB串行端口
    选取所有表单元素
    C# Bitmap类型与Byte[]类型相互转化
    本文介绍C# BitmapData
    C#多线程学习(六) 互斥对象
    C# List 复制克隆副本
  • 原文地址:https://www.cnblogs.com/nn369/p/7553464.html
Copyright © 2011-2022 走看看