excel创建

来源:互联网 发布:2016刷枪软件 编辑:程序博客网 时间:2024/05/20 21:18

package org.accp.getdatainfo.excel;

import jxl.*;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Boolean;
import jxl.write.Number;

import java.io.*;
import java.io.File.*;
import java.util.*;

public class CreateXLS {

 public static void main(String[] args) {
  //String targetfile = "E:/out.xlsx";// 输出的excel文件名
  String worksheet = "List";// 输出的excel文件工作表明
  String[] title = { "ID", "NAME", "DESCREIB" };
  WritableWorkbook workbook;
  try {
   // 创建可写入的Excel工作薄,运行时生成的文件在tomcat/bin下
   // workbook = Workbook.createWorkbook(new File("out.xlsx")
  // );
   System.out.println("begin");
   //OutputStream os = new FileOutputStream(targetfile);
   //workbook = Workbook.createWorkbook(os);
   workbook = Workbook.createWorkbook(new File("数据库结构显示图.xls"));
   WritableSheet sheet = workbook.createSheet(worksheet, 0);// 添加第一个工作表
    //WritableSheet sheet1 = workbook.createSheet(worksheet1, 1);//可添加第二个工作表
   /*
    * Label label = new Label(0, 2,"A label record");//put a label in
    * cell A3 Label(column,row); sheet.addCell(label);
    */
   Label label;
   for (int i = 0; i < title.length; i++) {
    // Label(列好,行号,内容);
    label = new Label(i, 0, title[i]);// put the title in row 1
    sheet.addCell(label);
   }
   Number number = new Number(3, 4, 3.14159);// put the number
              // 3.14159in cell D5
   sheet.addCell(number);
   // 添加带有字型Formatting 的对象
   WritableFont wf = new WritableFont(WritableFont.TIMES, 10,
     WritableFont.BOLD, true);
   WritableCellFormat wcfF = new WritableCellFormat(wf);
   Label labelCF = new Label(4, 4, "文本", wcfF);
   sheet.addCell(labelCF);
   // 添加带有字体颜色,带背景颜色Formatting 的对象
   WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
     WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
     Colour.RED);
   ;
   WritableCellFormat wcfFC = new WritableCellFormat(wfc);
   wcfFC.setBackground(Colour.BLUE);
   Label labelCFC = new Label(1, 5, "带颜色", wcfFC);
   sheet.addCell(labelCFC);
   // 添加带有formatting 的Number 对象
   NumberFormat nf = new NumberFormat("#.##");
   WritableCellFormat wcfN = new WritableCellFormat(nf);
   Number labelNF = new Number(1, 1, 3.1415926, wcfN);
   sheet.addCell(labelNF);
   // 添加boolean 的对象
   Boolean labelB = new Boolean(0, 3, false);
   sheet.addCell(labelB);
   // 添加DataTime对象
   DateTime labelDT = new DateTime(0, 3, new Date());
   sheet.addCell(labelDT);
   // 添加带有formatting的DateFormat对象
   DateFormat df = new DateFormat("ddMMyyyyhh:mm:ss");
   WritableCellFormat wcfDF = new WritableCellFormat(df);
   DateTime labelDTF = new DateTime(1, 3, new Date(), wcfDF);
   sheet.addCell(labelDTF);

   // 合并单元格
   // sheet.mergeCells(int col1, int row1, int col2, int row2);
   sheet.mergeCells(4, 5, 8, 10);
   wfc = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD,
     false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN);
   WritableCellFormat wchB = new WritableCellFormat(wfc);
   wchB.setAlignment(Alignment.CENTRE);
   labelCFC = new Label(4, 5, "合并单元格", wchB);
   sheet.addCell(labelCFC);
   // 设置边框
   WritableCellFormat wcsB = new WritableCellFormat();
   wcsB.setBorder(Border.ALL, BorderLineStyle.THICK);
   labelCFC = new Label(0, 6, "设置边框", wcsB);
   sheet.addCell(labelCFC);
   workbook.write();
   workbook.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
             System.out.println("end");
           Runtime r = Runtime.getRuntime();
           Process p = null;
           //Sftring cmd[]={"notepad","exec.java"};
           String cmd[]={"C:\\Program Files\\kingsoft\\WPS Office Personal\\office6\\et.exe","数据库结构显示图.xls"};
           try {
   p = r.exec(cmd);
  } catch (Exception e) {
            System.out.println("error executing:"+cmd[0]);
  }
 }

}

原创粉丝点击