excel创建

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]);
  }
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值