zoukankan      html  css  js  c++  java
  • java的jxl技术导入Excel

    项目结构:

    http://www.cnblogs.com/hongten/gallery/image/112177.html

    在项目中我们看到Reference Libraries中的jxl.jar包,它是我们自己外部引入的包。

    运行结果:

    http://www.cnblogs.com/hongten/gallery/image/112178.html

    ExcelHandle.java

      1 /**
    2 *
    3 */
    4 package com.b510;
    5
    6 import java.io.File;
    7
    8 import jxl.Workbook;
    9 import jxl.format.Border;
    10 import jxl.format.BorderLineStyle;
    11 import jxl.format.Colour;
    12 import jxl.write.Label;
    13 import jxl.write.WritableCellFormat;
    14 import jxl.write.WritableFont;
    15 import jxl.write.WritableSheet;
    16 import jxl.write.WritableWorkbook;
    17
    18 /**
    19 *
    20 * @author XHW
    21 *
    22 * @date 2012-2-29
    23 *
    24 */
    25 public class ExcelHandle {
    26
    27 /**
    28 * @param args
    29 */
    30 public static void main(String[] args) {
    31 ExcelHandle excelHandle = new ExcelHandle();
    32 excelHandle.writeExcel();
    33
    34 }
    35
    36 /**
    37 * 写入Excel
    38 *
    39 */
    40 public void writeExcel() {
    41 try {
    42 //写入到那个Excel文件 如:c:\\hello.xls,或者hello.xls(这个是在项目的根目录下)
    43 WritableWorkbook wwb = Workbook
    44 .createWorkbook(new File("hello.xls"));
    45 // 创建Excel工作表 指定名称和位置
    46 WritableSheet ws = wwb.createSheet("Test Sheet 1", 0);
    47 // 设置表格的列宽度
    48 ws.setColumnView(0, 14);//第一列宽14
    49 ws.setColumnView(1, 12);
    50 ws.setColumnView(2, 25);
    51 ws.setColumnView(3, 20);
    52 ws.setColumnView(4, 12);
    53 ws.setColumnView(5, 9);
    54 ws.setColumnView(6, 12);//第7列宽12
    55
    56 // **************往工作表中添加数据*****************
    57
    58 //定义字体格式:字体为:微软雅黑,24号子,加粗
    59 WritableFont titleFont = new WritableFont(WritableFont
    60 .createFont("微软雅黑"), 24, WritableFont.NO_BOLD);
    61 WritableFont contentFont = new WritableFont(WritableFont
    62 .createFont("楷体 _GB2312"), 12, WritableFont.NO_BOLD);
    63
    64 WritableCellFormat titleFormat = new WritableCellFormat(titleFont);
    65 WritableCellFormat contentFormat = new WritableCellFormat(
    66 contentFont);
    67 WritableCellFormat contentFormat2 = new WritableCellFormat(
    68 contentFont);
    69
    70 contentFormat.setBorder(Border.ALL, BorderLineStyle.THIN,
    71 Colour.BLACK);
    72 //设置格式居中对齐
    73 titleFormat.setAlignment(jxl.format.Alignment.CENTRE);
    74 contentFormat2.setAlignment(jxl.format.Alignment.CENTRE);
    75
    76 // ***************将定义好的单元格添加到工作表中*****************
    77 ws.mergeCells(0, 0, 6, 0);// 合并单元格A-G共7列
    78 ws.addCell(new Label(0, 0, "广州XXX大学2009级研究生课程考试成绩册", titleFormat));
    79 ws.addCell(new Label(0, 1, "课程名称", contentFormat2));
    80 ws.mergeCells(1, 1, 6, 1);// 合并单元格B-G共6列
    81 ws.addCell(new Label(1, 1, "大学数学", contentFormat2));
    82 ws.addCell(new Label(0, 2, "院所教研室", contentFormat2));
    83 ws.mergeCells(1, 2, 6, 2);// 合并单元格B-G共6列
    84 ws.addCell(new Label(0, 3, "填表人", contentFormat2));
    85 ws.addCell(new Label(2, 3, "教研室负责人", contentFormat2));
    86
    87 String th[] = { "学号", "姓名", "学院", "平时成绩", "期末成绩", "总成绩", "补考成绩" };
    88 for (int i = 0; i < 7; i++) {
    89 ws.addCell(new Label(i, 4, th[i], contentFormat2));
    90 }
    91 //这里我们可以从数据库里面查询数据,然后在这里获取数据
    92 int xh = 200901;
    93 String xm = "王佳佳";
    94 String xy = "XXX信息技术学院";
    95 String space = " ";
    96 int cj = 50;
    97 String bk = "补 80";
    98 //向Excel中插入数据
    99 for (int j = 5; j < 10; j++) {
    100 ws.addCell(new Label(0, j, "" + xh + j + "", contentFormat));
    101 ws.addCell(new Label(1, j, xm+j , contentFormat));
    102 ws.addCell(new Label(2, j, xy , contentFormat));
    103 ws.addCell(new Label(3, j, space , contentFormat));
    104 ws.addCell(new Label(4, j, space, contentFormat));
    105 ws.addCell(new Label(5, j, "" + cj + j + "", contentFormat));
    106 ws.addCell(new Label(6, j, "" + bk + "", contentFormat));
    107 }
    108 // 写入工作表完毕,关闭流
    109 wwb.write();
    110 wwb.close();
    111 } catch (Exception e) {
    112 e.printStackTrace();
    113 }
    114 }
    115
    116
    117
    118 }

    java的poi技术读取和导入Excel:

    http://www.cnblogs.com/hongten/archive/2012/02/22/java2poi.html

  • 相关阅读:
    thinkphp--标签库
    thinkphp中的参数绑定
    thinkphp3.2.3子查询中遇到的错误
    开篇马克
    BST树、B-树、B+树、B*树
    linux shell编程之变量和bash配置文件(第一篇)
    linux awk进阶篇
    linux awk(gawk)
    linux sed命令
    linux basic
  • 原文地址:https://www.cnblogs.com/hongten/p/java_jxl.html
Copyright © 2011-2022 走看看