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

📄 writablesheetcopier.java

📁 java操作Excel表格的api
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************
*
*      Copyright (C) 2006 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.write.biff;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.Iterator;

import common.Assert;
import common.Logger;

import jxl.BooleanCell;
import jxl.Cell;
import jxl.CellType;
import jxl.CellView;
import jxl.DateCell;
import jxl.HeaderFooter;
import jxl.Hyperlink;
import jxl.Image;
import jxl.LabelCell;
import jxl.NumberCell;
import jxl.Range;
import jxl.Sheet;
import jxl.SheetSettings;
import jxl.WorkbookSettings;
import jxl.biff.AutoFilter;
import jxl.biff.CellReferenceHelper;
import jxl.biff.ConditionalFormat;
import jxl.biff.DataValidation;
import jxl.biff.FormattingRecords;
import jxl.biff.FormulaData;
import jxl.biff.IndexMapping;
import jxl.biff.NumFormatRecordsException;
import jxl.biff.SheetRangeImpl;
import jxl.biff.WorkspaceInformationRecord;
import jxl.biff.XFRecord;
import jxl.biff.drawing.Chart;
import jxl.biff.drawing.ComboBox;
import jxl.biff.drawing.Drawing;
import jxl.biff.drawing.DrawingGroupObject;
import jxl.format.CellFormat;
import jxl.biff.formula.FormulaException;
import jxl.read.biff.SheetImpl;
import jxl.read.biff.NameRecord;
import jxl.read.biff.WorkbookParser;
import jxl.write.Blank;
import jxl.write.Boolean;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableHyperlink;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * A transient utility object used to copy sheets.   This 
 * functionality has been farmed out to a different class
 * in order to reduce the bloat of the WritableSheetImpl
 */
class WritableSheetCopier
{
  private static Logger logger = Logger.getLogger(SheetCopier.class);

  private WritableSheetImpl fromSheet;
  private WritableSheetImpl toSheet;
  private WorkbookSettings workbookSettings;

  // Objects used by the sheet
  private TreeSet fromColumnFormats;
  private TreeSet toColumnFormats;
  private MergedCells fromMergedCells;
  private MergedCells toMergedCells;
  private RowRecord[] fromRows;
  private ArrayList fromRowBreaks;
  private ArrayList fromColumnBreaks;
  private ArrayList toRowBreaks;
  private ArrayList toColumnBreaks;
  private DataValidation fromDataValidation;
  private DataValidation toDataValidation;
  private SheetWriter sheetWriter;
  private ArrayList fromDrawings;
  private ArrayList toDrawings;
  private ArrayList toImages;
  private WorkspaceInformationRecord fromWorkspaceOptions;
  private PLSRecord fromPLSRecord;
  private PLSRecord toPLSRecord;
  private ButtonPropertySetRecord fromButtonPropertySet;
  private ButtonPropertySetRecord toButtonPropertySet;
  private ArrayList fromHyperlinks;
  private ArrayList toHyperlinks;
  private int numRows;
  private int maxRowOutlineLevel;
  private int maxColumnOutlineLevel;


  private boolean chartOnly;
  private FormattingRecords formatRecords;



  // Objects used to maintain state during the copy process
  private HashMap xfRecords;
  private HashMap fonts;
  private HashMap formats;

  public WritableSheetCopier(WritableSheet f, WritableSheet t)
  {
    fromSheet = (WritableSheetImpl) f;
    toSheet = (WritableSheetImpl) t;
    workbookSettings = toSheet.getWorkbook().getSettings();
    chartOnly = false;
  }

  void setColumnFormats(TreeSet fcf, TreeSet tcf)
  {
    fromColumnFormats = fcf;
    toColumnFormats = tcf;
  }

  void setMergedCells(MergedCells fmc, MergedCells tmc)
  {
    fromMergedCells = fmc;
    toMergedCells = tmc;
  }

  void setRows(RowRecord[] r)
  {
    fromRows = r;
  }

  void setRowBreaks(ArrayList frb, ArrayList trb)
  {
    fromRowBreaks = frb;
    toRowBreaks = trb;
  }

  void setColumnBreaks(ArrayList fcb, ArrayList tcb)
  {
    fromColumnBreaks = fcb;
    toColumnBreaks = tcb;
  }

  void setDrawings(ArrayList fd, ArrayList td, ArrayList ti)
  {
    fromDrawings = fd;
    toDrawings = td;
    toImages = ti;
  }

  void setHyperlinks(ArrayList fh, ArrayList th)
  {
    fromHyperlinks = fh;
    toHyperlinks = th;
  }

  void setWorkspaceOptions(WorkspaceInformationRecord wir)
  {
    fromWorkspaceOptions = wir;
  }

  void setDataValidation(DataValidation dv)
  {
    fromDataValidation = dv;
  }

  void setPLSRecord(PLSRecord plsr)
  {
    fromPLSRecord = plsr;
  }

  void setButtonPropertySetRecord(ButtonPropertySetRecord bpsr)
  {
    fromButtonPropertySet = bpsr;
  }

  void setSheetWriter(SheetWriter sw)
  {
    sheetWriter = sw;
  }


  DataValidation getDataValidation()
  {
    return toDataValidation;
  }

  PLSRecord getPLSRecord()
  {
    return toPLSRecord;
  }

  boolean isChartOnly()
  {
    return chartOnly;
  }

  ButtonPropertySetRecord getButtonPropertySet()
  {
    return toButtonPropertySet;
  }

  /**
   * Copies a sheet from a read-only version to the writable version.
   * Performs shallow copies
   */
  public void copySheet()
  {
    shallowCopyCells();

    // Copy the column formats
    Iterator cfit = fromColumnFormats.iterator();
    while (cfit.hasNext())
    {
      ColumnInfoRecord cv = new ColumnInfoRecord
        ((ColumnInfoRecord) cfit.next());
      toColumnFormats.add(cv);
    }

    // Copy the merged cells
    Range[] merged = fromMergedCells.getMergedCells();

    for (int i = 0; i < merged.length; i++)
    {
      toMergedCells.add(new SheetRangeImpl((SheetRangeImpl)merged[i], 
                                           toSheet));
    }

    try
    {
      RowRecord row = null;
      RowRecord newRow = null;
      for (int i = 0; i < fromRows.length ; i++)
      {
        row = fromRows[i];
        
        if (row != null &&
            (!row.isDefaultHeight() ||
             row.isCollapsed()))
        {
          newRow = toSheet.getRowRecord(i);
          newRow.setRowDetails(row.getRowHeight(), 
                               row.matchesDefaultFontHeight(),
                               row.isCollapsed(),
                               row.getOutlineLevel(),
                               row.getGroupStart(),
                               row.getStyle());
        }
      }
    }
    catch (RowsExceededException e)
    {
      // Handle the rows exceeded exception - this cannot occur since
      // the sheet we are copying from will have a valid number of rows
      Assert.verify(false);
    }

    // Copy the horizontal page breaks
    toRowBreaks = new ArrayList(fromRowBreaks);

    // Copy the vertical page breaks
    toColumnBreaks = new ArrayList(fromColumnBreaks);

    // Copy the data validations
    if (fromDataValidation != null)
    {
      toDataValidation = new DataValidation
        (fromDataValidation, 
         toSheet.getWorkbook(),
         toSheet.getWorkbook(),
         toSheet.getWorkbook().getSettings());
    }

    // Copy the charts
    sheetWriter.setCharts(fromSheet.getCharts());

⌨️ 快捷键说明

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