jxl 实例

jxl官网:http://jexcelapi.sourceforge.net/

 

 

jexcelapi_2_6_12  

一开始就遇到问题了:Bad version number in .class file。版本不对? 

eclipse的preferences>Installed JREs里面添加一个jre6并且勾选中是使用该jre就好了

 

1.导出:

import java.io.File;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.Orientation;
import jxl.format.ScriptStyle;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFeatures;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Write {

 public void write() throws Exception {
  
  //WorkbookSettings ws = new WorkbookSettings();
    // ws.setLocale(new Locale("en", "EN"));
  WritableWorkbook workbook = Workbook.createWorkbook(new File("output_测试.xls"));//创建excel对象
  //WritableWorkbook workbook = Workbook.createWorkbook(new File("test.xls"), ws);
  
  
  WritableSheet sheet = workbook.createSheet("First Sheet", 0); //创建sheet
  //WritableSheet sheet2=workbook.createSheet("sheet2", 1);//创建第二个sheet
  
  //样式
  sheet.setColumnView(0, 60); //A宽度变宽了
  //sheet.setColumnView(1, 30); //B宽度也变宽了
  
  //格式化
    WritableFont arial12pt = new WritableFont(WritableFont.ARIAL, 12);//设置字体为12象素
      WritableCellFormat arial12format = new WritableCellFormat(arial12pt);
      arial12format.setWrap(true);
      Label labelGSH = new Label(4, 0, "Normal", arial12format);
      sheet.addCell(labelGSH);
  
  /* WritableFont a18p=new WritableFont(WritableFont.ARIAL,18);
    WritableCellFormat a18pf=new WritableCellFormat(a18p);
    a18pf.setWrap(true);
    Label label18=new Label(5,1,"18象素",a18pf);
    sheet.addCell(label18);
   */
    //  WritableFont arial12ptBold = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD); //字体12象素加粗
  
     /* WritableFont arial12ptItalic = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, true);//字体12象素斜体
      WritableCellFormat arial12ptItalicFormat = new WritableCellFormat(arial12ptItalic);
      Label labeli = new Label(4, 4, "Italic", arial12ptItalicFormat);
      sheet.addCell(labeli);
      */
     
     /* WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
      WritableCellFormat times10format = new WritableCellFormat(times10pt);
      Label labelt = new Label(0, 7, "Times Fonts", times10format);
      sheet.addCell(labelt);

      labelt = new Label(1, 7, "10pt", times10format);
      sheet.addCell(labelt);
   */
     
    /*  WritableFont arial10ptUnderline = new WritableFont(WritableFont.ARIAL,WritableFont.DEFAULT_POINT_SIZE,
         WritableFont.NO_BOLD,false,UnderlineStyle.SINGLE_ACCOUNTING);// 下划线 UnderlineStyle.DOUBLE(双下划线)
      WritableCellFormat arialUnderline = new WritableCellFormat(arial10ptUnderline);
      Label lr = new Label(6,11, "Underline", arialUnderline);
   sheet.addCell(lr);
     */
     
      //字体颜色
      /*WritableFont superscript = new WritableFont
        (WritableFont.ARIAL,
         WritableFont.DEFAULT_POINT_SIZE,
         WritableFont.NO_BOLD,
         false,
         UnderlineStyle.NO_UNDERLINE,
         Colour.RED,
         ScriptStyle.SUPERSCRIPT);
      WritableCellFormat superscriptFormat = new WritableCellFormat
        (superscript);
     Label lr = new Label(1,15, "superscript", superscriptFormat);
      sheet.addCell(lr);
     */
     
      //背景颜色
     /* WritableCellFormat greyBackground = new WritableCellFormat();
      greyBackground.setWrap(true);
      greyBackground.setBackground(Colour.GRAY_50);
      Label labelg = new Label(1, 16, "Grey background", greyBackground);
      sheet.addCell(labelg);
   */
     
     
  Label label = new Label(0, 2, "A label record"); //添加一个Label 在A3
  sheet.addCell(label);  
 
  Label lrn = new Label(1,17,
                "A very long label, more than 255 characters\012" +
                "Rejoice O shores\012"+
                "Sing O bells\012" +
                "But I with mournful tread\012" +
                "Walk the deck my captain lies\012" +
                "Fallen cold and dead\012"+
                "Summer surprised, coming over the Starnbergersee\012" +
                "With a shower of rain. We stopped in the Colonnade\012" +
                "A very long label, more than 255 characters\012" +
                "Rejoice O shores\012" +
                "Sing O bells\012" +
                "But I with mournful tread\012" +
                "Walk the deck my captain lies\012" +
                "Fallen cold and dead\012"+
                "Summer surprised, coming over the Starnbergersee\012" +
                "With a shower of rain. We stopped in the Colonnade\012" +          
                "And sat and drank coffee an talked for an hour\012",
                arial12format);    //\012换行,内容多的话推荐使用这个
   sheet.addCell(lrn);
  
   /*//垂直显示
      WritableCellFormat vertical = new WritableCellFormat();
       vertical.setOrientation(Orientation.VERTICAL);
       Label lrv = new Label(0, 12, "Vertical orientation", vertical);
       sheet.addCell(lrv);
       */
   
   /*//旋转45度
    * WritableCellFormat plus_90 = new WritableCellFormat();
      plus_90.setOrientation(Orientation.PLUS_45);
      Label lrp = new Label(0, 13, "Plus 90", plus_90);
      sheet.addCell(lrp);
   */
   
   /*合并单元格
    WritableCellFormat wcf = new WritableCellFormat();
       wcf.setAlignment(Alignment.CENTRE);
       Label l2 = new Label(5, 42, "Centred across merged cells合并成一行", wcf);
       sheet.addCell(l2);
       sheet.mergeCells(5, 42, 10, 42);  //合并成一行
       //sheet.mergeCells(6, 42, 6, 46);  //合并成一列
       //sheet.mergeCells(5, 42, 6, 46);   //合并成多行多列
   */
   WritableCellFormat wcf = new WritableCellFormat();
      wcf.setAlignment(Alignment.CENTRE);
      Label l2 = new Label(5, 42, "Centred across merged cells", wcf);
      sheet.addCell(l2);
      sheet.mergeCells(5, 42, 6, 46);  //合并成多行多列
     
      WritableCellFormat wcf2 = new WritableCellFormat();
      wcf2.setBorder(Border.ALL, BorderLineStyle.THIN);          //边框
      wcf2.setBackground(Colour.GRAY_25);     //背景
      Label le = new Label(3, 44, "Merged with border", wcf2);
      sheet.addCell(le);
      sheet.mergeCells(3, 44, 4, 46);
   
      WritableFont times12pt = new WritableFont(WritableFont.TIMES, 12);
      WritableCellFormat indented = new WritableCellFormat(times12pt);
      indented.setIndentation(2);         //缩进
      Label l5 = new Label(0, 31, "Some indented text", indented);
      sheet.addCell(l5);
     
    Label l = new Label(0, 14, "A cell with a comment");
       WritableCellFeatures cellFeatures = new WritableCellFeatures();
       cellFeatures.setComment("the cell comment");     //备注
       l.setCellFeatures(cellFeatures);
       sheet.addCell(l);

 

  Date date=new Date();

       // Built in date formats
      // SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss.SSS");
       SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日   mm:ss");
       sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
       Label lr = new Label(0,0,"All dates are " + sdf.format(date),wrappedText);
       sheet.addCell(lr);
      
       //数值格式化
       WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
       Number n = new Number(1,2,3.1415926535,cf2);  //3.14
       sheet.addCell(n);
      
       NumberFormat dp3 = new NumberFormat("###,###.##");   //0.000E0    #.###    0.0%
       WritableCellFormat dp3cell = new WritableCellFormat(dp3);
       Number n2 = new Number(1,3,3.1415926535,dp3cell);
       sheet.addCell(n2);
      
       //Boolean
      jxl.write.Boolean b=new jxl.write.Boolean(1,4,true);
      sheet.addCell(b);
     
   
  Number number = new Number(3, 4, 3.1459);         //添加数值:3.1459在D5
  sheet.addCell(number);
  
  //Label label2=new Label(0,3,"测试");
  //sheet.addCell(label2);

 

 //添加图片对象,jxl只支持png格式图片  
                  File   image   =   new   File("d:\\我的文档\\供应商.png");  
                  WritableImage   wimage   =   new   WritableImage(0,7,20,20,image);//0,1分别代表x,y.20,20代表宽和高占的单元格数  
                    sheet.addImage(wimage);
  
  workbook.write();
  workbook.close();
 }
 public static void main(String[] args) {
  Write jxl=new Write();
  try {
   jxl.write();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

 

你可能感兴趣的:(eclipse,Excel,单元测试,WCF)