⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 write.java

📁 一个非常有用的操作MCRSOFT EXCEL文件的工具。可以用JAVA方便的新建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*********************************************************************
*
*      Copyright (C) 2001 Andrew Khan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/

package jxl.demo;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.net.URL;
import java.net.MalformedURLException;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.Range;
import jxl.CellView;
import jxl.CellReferenceHelper;
import jxl.HeaderFooter;
import jxl.write.WritableWorkbook;
import jxl.write.WritableSheet;
import jxl.write.WritableFont;
import jxl.write.WritableCellFormat;
import jxl.write.NumberFormats;
import jxl.write.DateFormats;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.DateTime;
import jxl.write.NumberFormat;
import jxl.write.DateFormat;
import jxl.write.WriteException;
import jxl.write.WritableHyperlink;
import jxl.write.Boolean;
import jxl.write.Formula;
import jxl.write.WritableImage;
import jxl.write.WritableCellFeatures;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.ScriptStyle;
import jxl.format.Orientation;
import jxl.format.PageOrientation;
import jxl.format.PaperSize;

/**
 * Demo class which writes a spreadsheet.  This demo illustrates most of the
 * features of the JExcelAPI, such as text, numbers, fonts, number formats and
 * date formats
 */
public class Write
{
  /**
   * The filename
   */
  private String filename;

  /**
   * The workbook
   */
  private WritableWorkbook workbook;

  /**
   * Constructor
   * 
   * @param fn 
   */
  public Write(String fn)
  {
    filename = fn;
  }

  /**
   * Uses the JExcelAPI to create a spreadsheet
   * 
   * @exception IOException
   * @exception WriteException
   */
  public void write() throws IOException, WriteException
  {
    WorkbookSettings ws = new WorkbookSettings();
    ws.setLocale(new Locale("en", "EN"));
    workbook = Workbook.createWorkbook(new File(filename), ws);


    WritableSheet s2 = workbook.createSheet("Number Formats", 0);
    WritableSheet s3 = workbook.createSheet("Date Formats", 1);
    WritableSheet s1 = workbook.createSheet("Label Formats", 2);
    WritableSheet s4 = workbook.createSheet("Borders", 3);
    WritableSheet s5 = workbook.createSheet("Labels", 4);
    WritableSheet s6 = workbook.createSheet("Formulas", 5);
    WritableSheet s7 = workbook.createSheet("Images", 6);
    //WritableSheet s8 = workbook.createSheet
    //  ("'Illegal chars in name !*%^?': which exceeds max name length",7);

    writeLabelFormatSheet(s1);
    writeNumberFormatSheet(s2);
    writeDateFormatSheet(s3);
    writeBordersSheet(s4);
    writeLabelsSheet(s5);
    writeFormulaSheet(s6);
    writeImageSheet(s7);

    // Modify the colour palette to bright red for the lime colour
    workbook.setColourRGB(Colour.LIME, 0xff, 0, 0);

    workbook.write();
    workbook.close();
  }
  
  /**
   * Writes out a sheet containing the various numerical formats
   * 
   * @param s 
   */
  private void writeNumberFormatSheet(WritableSheet s) throws WriteException
  {
    WritableCellFormat wrappedText = new WritableCellFormat
      (WritableWorkbook.ARIAL_10_PT);
    wrappedText.setWrap(true);

    s.setColumnView(0,20);
    s.setColumnView(4,20);
    s.setColumnView(5,20);
    s.setColumnView(6,20);

    // Floats
    Label l = new Label(0,0,"+/- Pi - default format", wrappedText);
    s.addCell(l);

    Number n = new Number(1,0,3.1415926535);
    s.addCell(n);

    n = new Number(2,0,-3.1415926535);
    s.addCell(n);

    l = new Label(0,1,"+/- Pi - integer format", wrappedText);
    s.addCell(l);

    WritableCellFormat cf1 = new WritableCellFormat(NumberFormats.INTEGER);
    n = new Number(1,1,3.1415926535,cf1);
    s.addCell(n);

    n = new Number(2,1,-3.1415926535, cf1);
    s.addCell(n);

    l = new Label(0,2,"+/- Pi - float 2dps", wrappedText);
    s.addCell(l);

    WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT);
    n = new Number(1,2,3.1415926535,cf2);
    s.addCell(n);

    n = new Number(2,2,-3.1415926535, cf2);
    s.addCell(n);

    l = new Label(0,3,"+/- Pi - custom 3dps", 
                  wrappedText);
    s.addCell(l);

    NumberFormat dp3 = new NumberFormat("#.###");
    WritableCellFormat dp3cell = new WritableCellFormat(dp3);
    n = new Number(1,3,3.1415926535,dp3cell);
    s.addCell(n);

    n = new Number(2,3,-3.1415926535, dp3cell);
    s.addCell(n);

    l = new Label(0,4,"+/- Pi - custom &3.14", 
                  wrappedText);
    s.addCell(l);

    NumberFormat pounddp2 = new NumberFormat("&#.00");
    WritableCellFormat pounddp2cell = new WritableCellFormat(pounddp2);
    n = new Number(1,4,3.1415926535,pounddp2cell);
    s.addCell(n);

    n = new Number(2,4,-3.1415926535, pounddp2cell);
    s.addCell(n);

    l = new Label(0,5,"+/- Pi - custom Text #.### Text", 
                  wrappedText);
    s.addCell(l);

    NumberFormat textdp4 = new NumberFormat("Text#.####Text");
    WritableCellFormat textdp4cell = new WritableCellFormat(textdp4);
    n = new Number(1,5,3.1415926535, textdp4cell);
    s.addCell(n);

    n = new Number(2,5,-3.1415926535, textdp4cell);
    s.addCell(n);

    // Integers
    l = new Label(4,0,"+/- Bilko default format");
    s.addCell(l);
    n = new Number(5, 0, 15042699);
    s.addCell(n);
    n = new Number(6, 0, -15042699);
    s.addCell(n);

    l = new Label(4,1,"+/- Bilko float format");
    s.addCell(l);
    WritableCellFormat cfi1 = new WritableCellFormat(NumberFormats.FLOAT);
    n = new Number(5, 1, 15042699, cfi1);
    s.addCell(n);
    n = new Number(6, 1, -15042699, cfi1);
    s.addCell(n);

    l = new Label(4,2,"+/- Thousands separator");
    s.addCell(l);
    WritableCellFormat cfi2 = new WritableCellFormat
      (NumberFormats.THOUSANDS_INTEGER);
    n = new Number(5, 2, 15042699,cfi2 );
    s.addCell(n);
    n = new Number(6, 2, -15042699, cfi2);
    s.addCell(n);

    l = new Label(4,3,"+/- Accounting red - added 0.01");
    s.addCell(l);
    WritableCellFormat cfi3 = new WritableCellFormat
      (NumberFormats.ACCOUNTING_RED_FLOAT);
    n = new Number(5, 3, 15042699.01, cfi3);
    s.addCell(n);
    n = new Number(6, 3, -15042699.01, cfi3);
    s.addCell(n);

    l = new Label(4,4,"+/- Percent");
    s.addCell(l);
    WritableCellFormat cfi4 = new WritableCellFormat
      (NumberFormats.PERCENT_INTEGER);
    n = new Number(5, 4, 15042699, cfi4);
    s.addCell(n);
    n = new Number(6, 4, -15042699, cfi4);
    s.addCell(n);

    l = new Label(4,5,"+/- Exponential - 2dps");
    s.addCell(l);
    WritableCellFormat cfi5 = new WritableCellFormat
      (NumberFormats.EXPONENTIAL);
    n = new Number(5, 5, 15042699, cfi5);
    s.addCell(n);
    n = new Number(6, 5, -15042699, cfi5);
    s.addCell(n);

    l = new Label(4,6,"+/- Custom exponentional - 3dps", wrappedText);
    s.addCell(l);
    NumberFormat edp3 = new NumberFormat("0.000E0");
    WritableCellFormat edp3Cell = new WritableCellFormat(edp3);
    n = new Number(5,6,15042699,edp3Cell);
    s.addCell(n);
    n = new Number(6,6,-15042699,edp3Cell);
    s.addCell(n);

    l = new Label(4, 7, "Custom neg brackets", wrappedText);
    s.addCell(l);
    NumberFormat negbracks = new NumberFormat("#,##0;(#,##0)");
    WritableCellFormat negbrackscell = new WritableCellFormat(negbracks);
    n = new Number(5,7, 15042699, negbrackscell);
    s.addCell(n);
    n = new Number(6,7, -15042699, negbrackscell);
    s.addCell(n);

    l = new Label(4, 8, "Custom neg brackets 2", wrappedText);
    s.addCell(l);
    NumberFormat negbracks2 = new NumberFormat("#,##0;(#,##0)a");
    WritableCellFormat negbrackscell2 = new WritableCellFormat(negbracks2);
    n = new Number(5,8, 15042699, negbrackscell2);
    s.addCell(n);
    n = new Number(6,8, -15042699, negbrackscell2);
    s.addCell(n);

    l = new Label(4, 9, "Custom percent", wrappedText);
    s.addCell(l);
    NumberFormat cuspercent = new NumberFormat("0.0%");
    WritableCellFormat cuspercentf = new WritableCellFormat(cuspercent);
    n = new Number(5, 9, 3.14159265, cuspercentf);
    s.addCell(n);
    

    // Booleans
    l = new Label(0,10, "Boolean - TRUE");
    s.addCell(l);
    Boolean b = new Boolean(1,10, true);
    s.addCell(b);

    l = new Label(0,11, "Boolean - FALSE");
    s.addCell(l);
    b = new Boolean(1,11,false);
    s.addCell(b);

    l = new Label(0, 12, "A hidden cell->");
    s.addCell(l);
    n = new Number(1, 12, 17, WritableWorkbook.HIDDEN_STYLE);
    s.addCell(n);


    // Lots of numbers
    for (int row = 0; row < 100; row++)
    {
      for (int col = 8; col < 108; col++)
      {
        n = new Number(col, row, col+row);
        s.addCell(n);
      }
    }

    // Lots of numbers
    for (int row = 101; row < 3000; row++)
    {
      for (int col = 0; col < 25; col++)
      {
        n = new Number(col, row, col+row);
        s.addCell(n);
      }
    }
  }

  /**
   * Adds cells to the specified sheet which test the various date formats
   * 
   * @param s 
   */
  private void writeDateFormatSheet(WritableSheet s) throws WriteException
  {
    WritableCellFormat wrappedText = new WritableCellFormat
      (WritableWorkbook.ARIAL_10_PT);
    wrappedText.setWrap(true);

    s.setColumnView(0, 20);
    s.setColumnView(2, 20);
    s.setColumnView(3, 20);
    s.setColumnView(4, 20);

    s.getSettings().setFitWidth(2);
    s.getSettings().setFitHeight(2);

    Calendar c = Calendar.getInstance();
    c.set(1975, 4, 31, 15, 21, 45);
    c.set(Calendar.MILLISECOND, 660);
    Date date = c.getTime();
    c.set(1900, 0, 1, 0, 0, 0);
    c.set(Calendar.MILLISECOND, 0);

    Date date2 = c.getTime();
    Date date3 = new Date(0);
    c.set(1918, 10, 11, 10, 0, 0);
    Date date4 = c.getTime();
    c.set(1900, 0, 2, 0, 0, 0);
    Date date5 = c.getTime();
    c.set(1901, 0, 1, 0, 0, 0);
    Date date6 = c.getTime();
    c.set(1900, 4, 31, 0, 0, 0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -