JXCELL实例学习与研究(七) 之 JAVA的GUI对JXCELL的小客户端,显示各种表格状态的方法

代码比较长,由两段代码组成。Format.java用于绘制布局调用消息,CellFrmt.java用于对EXCEL进行绘制,代码有点BUG,先看核心的绘制代码吧。


import com.jxcell.CellFormat;
import com.jxcell.RangeRef;
import com.jxcell.View;

import java.awt.*;

public class CellFrmt
{ 
    //Global constants for the Format types
    public final int SIMPLE = 0;
    public final int CLASSIC1 = 1;
    public final int CLASSIC2 = 2;
    public final int CLASSIC3 = 3;
    public final int ACCOUNTING1 = 4;
    public final int ACCOUNTING2 = 5;
    public final int ACCOUNTING3 = 6;
    public final int LIST1 = 7;
    public final int LIST2 = 8;
    public final int LIST3 = 9;
    public final int COLORFUL1 = 10;
    public final int COLORFUL2 = 11;
    public final int COLORFUL3 = 12;
    public final int EFFECTS3D1 = 13;

    View m_view;
    CellFormat cfmt;
    
    CellFrmt(View view1)
    {
            m_view = view1;

            try{
                m_view.setSelection(1,1,7,5);
            }
            catch (com.jxcell.CellException e){
                System.out.println(e.getMessage());
            }
    }
    
    String hdrRange = null;
    String  ftrRange = null, 
            colRange = null, 
            bodyRange = null, 
            eRange,
            StartRange;
    RangeRef savedSelection = null;
    
    //FrmtType -- format type ie Simple, Accounting etc.
    public void FormatCells(int FrmtType){
        
        int StartRow = 0, StartCol = 0 , EndRow = 0, EndCol = 0;
        String fileName, dirName;
              
        dirName = System.getProperty("user.dir");
        fileName = "book.xls";

        try
        {
            m_view.read(dirName + "\\" + fileName);
            m_view.setSelection(1,1,7,5);
            m_view.setShowGridLines(false);
            m_view.setShowEditBar(false);
            m_view.setShowRowHeading(false);
            m_view.setShowColHeading(false);
            m_view.setShowTabs((short)0);
            cfmt = m_view.getCellFormat();
            m_view.editClear(View.ClearFormats);

            savedSelection = m_view.getSelection();

        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
        catch (java.io.IOException io){
              System.out.println(io.getMessage());
        }

        try
        {
            StartRow = m_view.getSelStartRow();
            StartCol = m_view.getSelStartCol();
            EndRow = m_view.getSelEndRow();
            EndCol = m_view.getSelEndCol();
            StartRange = m_view.formatRCNr(StartRow, StartCol, false);
            eRange = m_view.formatRCNr(StartRow, EndCol, false);
            hdrRange = StartRange + ":" + eRange;
            
            eRange = m_view.formatRCNr(EndRow, StartCol, false);
            colRange = StartRange + ":" + eRange;
            
            StartRange = m_view.formatRCNr(EndRow, StartCol, false);
            eRange = m_view.formatRCNr(EndRow, EndCol, false);
            ftrRange = StartRange + ":" + eRange;
            
            StartRange = m_view.formatRCNr(StartRow + 1, StartCol + 1, false);
            eRange = m_view.formatRCNr(EndRow- 1, EndCol, false);
            bodyRange = StartRange + ":" + eRange;
            
                
            m_view.setSelection (hdrRange);
            cfmt.setTopBorder(CellFormat.BorderMedium);
            cfmt.setBottomBorder(CellFormat.BorderMedium);
            AdjustFont(java.awt.Color.black.getRGB(), true, false, false, cfmt);
            cfmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentRight);
            cfmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
               
            m_view.setCellFormat(cfmt);
            //m_view.update();
              
            m_view.setSelection (ftrRange);
            cfmt.setBottomBorder(CellFormat.BorderMedium);
            cfmt.setTopBorder(CellFormat.BorderThin);
            m_view.setCellFormat(cfmt);
            //m_view.update();
            
               
            }
        catch (com.jxcell.CellException e){
                System.out.println("Can't set border " + e.getMessage());
        }
        
        if (FrmtType == SIMPLE)
        {
            try
            {
                m_view.setSelection(colRange);
                m_view.setCellFormat(cfmt);

                m_view.setSelection (hdrRange);
               cfmt.setTopBorder(CellFormat.BorderMedium);
               cfmt.setBottomBorder(CellFormat.BorderMedium);
               AdjustFont(java.awt.Color.black.getRGB(), true, false, false, cfmt);
               cfmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentRight);
               cfmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
               m_view.setCellFormat(cfmt);
               //m_view.update();
               
               m_view.setSelection (ftrRange);
               cfmt.setBottomBorder(CellFormat.BorderMedium);
               cfmt.setTopBorder(CellFormat.BorderThin);
               m_view.setCellFormat(cfmt);
               //m_view.update();
               
            }
            catch (com.jxcell.CellException e)
            {
                System.out.println("Can't set border " + e.getMessage());
            }
        }
        else if (FrmtType == CLASSIC1)
        {
            
            try{
                
                m_view.setSelection(colRange);
                cfmt.setTopBorder(CellFormat.BorderNone);
                cfmt.setBottomBorder(CellFormat.BorderNone);
                cfmt.setRightBorder(CellFormat.BorderThin);
                m_view.setCellFormat(cfmt);
                //m_view.update();

                cfmt.setRightBorder(CellFormat.BorderNone);
                m_view.setSelection(hdrRange);
                cfmt.setTopBorder(CellFormat.BorderMedium);
                cfmt.setBottomBorder(CellFormat.BorderThin);
                AdjustFont(java.awt.Color.black.getRGB(), false, true, false, cfmt);
                cfmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentRight);
                cfmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
                m_view.setCellFormat(cfmt);
                //m_view.update();
                
                m_view.setSelection(ftrRange);
                cfmt.setTopBorder(CellFormat.BorderThin);
                cfmt.setBottomBorder(CellFormat.BorderMedium);
                m_view.setCellFormat(cfmt);
               // m_view.update();
                
            }
            catch (com.jxcell.CellException e){
                System.out.println(e.getMessage());
            }

        }
        else if (FrmtType == CLASSIC2)
        {
            Classic2(cfmt);
        }
        else if (FrmtType == CLASSIC3)
        {
            Classic3(cfmt);
        }
        else if (FrmtType == ACCOUNTING1)
        {   
            Accounting1(cfmt);
        }
        else if (FrmtType == ACCOUNTING2)
        {
            Accounting2(cfmt);
        }
        else if (FrmtType == ACCOUNTING3)
        {
            Accounting3(cfmt);
        }
        else if (FrmtType == LIST1)
        {
            List1(cfmt);
        }
        else if (FrmtType == LIST2)
        {
            List2(cfmt);
        }
        else if (FrmtType == LIST3)
        {
            List3(cfmt);
        }
        else if (FrmtType == COLORFUL1)
        {
            Colorful1(cfmt);
        }
        else if (FrmtType == COLORFUL2)
        {
            Colorful2(cfmt);
        }
        else if (FrmtType == COLORFUL3)
        {
            Colorful3(cfmt);
        }
        
        else if (FrmtType == EFFECTS3D1)
        {
            Effects3D1(cfmt);
        }

        m_view.setRepaint(true);
        m_view.repaint();
        
    }//FormatCells
 
    private void Classic2(CellFormat cellFmt){
        
        short nPattern;
        try{
            
           m_view.setSelection(colRange);
           AdjustFont(java.awt.Color.black.getRGB(), true, false, false, cellFmt);
           m_view.setCellFormat(cellFmt);
           //m_view.update();
            
           m_view.setSelection(hdrRange);
           cellFmt.setTopBorder(CellFormat.BorderMedium);
           cellFmt.setBottomBorder(CellFormat.BorderThin);
           AdjustFont(java.awt.Color.white.getRGB(), false, false, false, cellFmt);
           nPattern = 1;
           cellFmt.setPattern(nPattern);
           cellFmt.setPatternFG(java.awt.Color.magenta);
           cellFmt.setPatternBG(java.awt.Color.magenta);
           cellFmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentRight);
           cellFmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
           m_view.setCellFormat(cellFmt);
           //m_view.update();

           m_view.setSelection(ftrRange);
           cellFmt.setBottomBorder(CellFormat.BorderMedium);
           cellFmt.setTopBorder(CellFormat.BorderThin);
           m_view.setCellFormat(cellFmt);
           //m_view.update();
           
                
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
        
    } //Classic2
    
    private void Classic3 (CellFormat cellFmt){

        try{
            m_view.setSelection(hdrRange);
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            AdjustFont(java.awt.Color.white.getRGB(), true, true, false, cellFmt);
            AlignRight(cellFmt);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(11).getRGB(), java.awt.Color.black.getRGB());
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(ftrRange);
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(15).getRGB(), java.awt.Color.black.getRGB());
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(bodyRange);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(15).getRGB(), java.awt.Color.black.getRGB());
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(colRange);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(15).getRGB(), java.awt.Color.black.getRGB());
            m_view.setCellFormat(cellFmt);
            //m_view.update();


        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
  
    }//classic3
    
    
    private void Accounting1(CellFormat cellFmt)
    {
        String numberFormat;
        try{
            m_view.setSelection(colRange);
            m_view.setCellFormat(cfmt);

            m_view.setSelection(hdrRange);
            cellFmt.setTopBorder(CellFormat.BorderThin);
            cellFmt.setBottomBorder(CellFormat.BorderThin);
            AdjustFont(java.awt.Color.magenta.getRGB(), true, true, false, cellFmt);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(bodyRange);
            numberFormat = "#,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(savedSelection);
            m_view.setSelection(m_view.getSelStartRow() + 1, m_view.getSelStartCol()+1,
                            m_view.getSelStartRow() + 1, m_view.getSelEndCol());
            cellFmt.setBottomBorder(CellFormat.BorderNone);
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(ftrRange);
            cellFmt.setBottomBorder(CellFormat.BorderDouble);
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    } //Accounting 1
    
    
    private void Accounting2(CellFormat cellFmt)
    {
       
        String numberFormat;

        try{
            m_view.setSelection(colRange);
            m_view.setCellFormat(cfmt);

            m_view.setSelection(hdrRange);
            cellFmt.setTopBorder(CellFormat.BorderThick);
            cellFmt.setTopBorderColor(java.awt.Color.lightGray);
            cellFmt.setBottomBorder(CellFormat.BorderThin);
            cellFmt.setBottomBorderColor(java.awt.Color.lightGray);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(bodyRange);
            numberFormat = "#,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(savedSelection);
            m_view.setSelection(m_view.getSelStartRow() + 1, m_view.getSelStartCol()+1,
                            m_view.getSelStartRow() + 1, m_view.getSelEndCol());
            cellFmt.setBottomBorder(CellFormat.BorderNone);
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

            m_view.setSelection(ftrRange);
            cellFmt.setBottomBorder(CellFormat.BorderThick);
            cellFmt.setBottomBorderColor(java.awt.Color.lightGray);
            cellFmt.setTopBorder(CellFormat.BorderThin);
            cellFmt.setTopBorderColor(java.awt.Color.lightGray);
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();

        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
        
    } //Accounting2
    
    private void Accounting3(CellFormat cellFmt)
    {
        String numberFormat;
        try{
            
            m_view.setSelection(colRange);
            AdjustFont(java.awt.Color.black.getRGB(), false, true, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(bodyRange);
            numberFormat = "#,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(savedSelection);
            m_view.setSelection(m_view.getSelStartRow() + 1, m_view.getSelStartCol() + 1,
                            m_view.getSelStartRow() + 1, m_view.getSelEndCol());
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setTopBorder(CellFormat.BorderNone);
            cellFmt.setBottomBorder(CellFormat.BorderNone);
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(ftrRange);
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(m_view.getSelEndRow(), m_view.getSelStartCol()+1,
                            m_view.getSelEndRow(), m_view.getSelEndCol());
            cellFmt.setTopBorder(CellFormat.BorderThin);
            cellFmt.setBottomBorder(CellFormat.BorderDouble);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
            m_view.setSelection(savedSelection);
            m_view.setSelection(m_view.getSelStartRow(), m_view.getSelStartCol(),
                            m_view.getSelStartRow(), m_view.getSelEndCol());
            cellFmt.setTopBorder(CellFormat.BorderNone);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorderColor(java.awt.Color.green);
            AdjustFont(m_view.getPaletteEntry(16).getRGB(), false, true, false, cellFmt);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            //m_view.update();
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    } //Accounting3   
    
    
    private void Effects3D1(CellFormat cellFmt)
    {
        try{
            SetSolidPattern(cellFmt, java.awt.Color.lightGray.getRGB(),0);

            Set3DBorder(cellFmt, m_view.getSelStartRow(), m_view.getSelStartCol(),
                          m_view.getSelEndRow(), m_view.getSelEndCol(),
                          java.awt.Color.darkGray.getRGB(), java.awt.Color.darkGray.getRGB(),java.awt.Color.darkGray.getRGB());
            m_view.setCellFormat(cellFmt);

            m_view.setSelection(bodyRange);
            Set3DBorder(cellFmt, m_view.getSelStartRow(), m_view.getSelStartCol(),
                          m_view.getSelEndRow(), m_view.getSelEndCol(),
                          java.awt.Color.lightGray.getRGB(), java.awt.Color.darkGray.getRGB(), java.awt.Color.lightGray.getRGB());
            m_view.setCellFormat(cellFmt);
         
            m_view.setSelection(hdrRange);
            AdjustFont(java.awt.Color.magenta.getRGB(), true, false, false, cellFmt);
            AlignCenter(cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(colRange);
            Set3DBorder(cellFmt,m_view.getSelStartRow(),m_view.getSelStartCol(),m_view.getSelEndRow(), m_view.getSelEndCol(), java.awt.Color.lightGray.getRGB(), java.awt.Color.lightGray.getRGB(),java.awt.Color.darkGray.getRGB());
            AdjustFont(java.awt.Color.black.getRGB(), true, false, false, cellFmt);
            m_view.setCellFormat(cellFmt);

            m_view.setSelection(ftrRange);
            Set3DBorder(cellFmt,m_view.getSelStartRow(),m_view.getSelStartCol(),m_view.getSelEndRow(),m_view.getSelEndCol(), java.awt.Color.lightGray.getRGB(), java.awt.Color.darkGray.getRGB(), java.awt.Color.darkGray.getRGB());
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
        
    }//Effects3D1       
          
    private void Colorful1(CellFormat cellFmt)
    {
        int color = java.awt.Color.red.getRGB();
        try{
            m_view.setSelection(savedSelection);
            cellFmt.setBottomBorder(CellFormat.BorderThin);
            cellFmt.setBottomBorderColor(java.awt.Color.red);
            SetSolidPattern(cellFmt,java.awt.Color.darkGray.getRGB(),java.awt.Color.black.getRGB());

            //outline border
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setLeftBorder(CellFormat.BorderMedium);
            cellFmt.setRightBorder(CellFormat.BorderMedium);
            cellFmt.setTopBorderColor(java.awt.Color.red);
            cellFmt.setLeftBorderColor(java.awt.Color.red);
            cellFmt.setRightBorderColor(java.awt.Color.red);
            
            AdjustFont(color, false, false, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(hdrRange);
            SetSolidPattern(cellFmt, java.awt.Color.black.getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(color, true, true, false, cellFmt);
            AlignCenter(cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(colRange);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(11).getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(color, true, true, false, cellFmt);
            m_view.setCellFormat(cellFmt);
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }//Colorful1
    
    private void Colorful2(CellFormat cellFmt)
    {

        
        try{
            int color = m_view.getPaletteEntry(14).getRGB();
            m_view.setSelection(hdrRange);
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorder(CellFormat.BorderThin);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(9).getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(color, true, true, false, cellFmt);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(ftrRange);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            cellFmt.setTopBorder(CellFormat.BorderThin);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(colRange);
            AdjustFont(java.awt.Color.black.getRGB(), true, true, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(savedSelection);
            SetHatchPattern(cellFmt, m_view.getPaletteEntry(16).getRGB(), java.awt.Color.red.getRGB());
            m_view.setCellFormat(cellFmt);
            
        }
        
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }

    }//Colorful2
    
    private void Colorful3(CellFormat cellFmt)
    {
        try{
            m_view.setSelection(savedSelection);
            SetSolidPattern(cellFmt, java.awt.Color.black.getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(java.awt.Color.white.getRGB(), false, false, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(hdrRange);
            
            AdjustFont(java.awt.Color.green.getRGB(), true, true, false, cellFmt);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(colRange);
            AdjustFont(java.awt.Color.magenta.getRGB(), true, true, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }//Colorful3
    
    private void List1(CellFormat cellFmt)
    {
        String newSelection, numberFormat;
        int fcolor, bcolor;
        
        try{
            m_view.setSelection(savedSelection);
            //Outline Border - thin
            cellFmt.setTopBorder(CellFormat.BorderThin);
            cellFmt.setLeftBorder(CellFormat.BorderThin);
            cellFmt.setRightBorder(CellFormat.BorderThin);
            cellFmt.setTopBorderColor(m_view.getPaletteEntry(16));
            cellFmt.setLeftBorderColor(m_view.getPaletteEntry(16));
            cellFmt.setRightBorderColor(m_view.getPaletteEntry(16));
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(hdrRange);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(14).getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(java.awt.Color.blue.getRGB(), true, true, false, cellFmt);
            AlignCenter(cellFmt);
            m_view.setCellFormat(cellFmt);
    
            m_view.setSelection(ftrRange);
            SetSolidPattern(cellFmt, m_view.getPaletteEntry(14).getRGB(),java.awt.Color.black.getRGB());
            AdjustFont(java.awt.Color.blue.getRGB(), true, false, false, cellFmt);
            numberFormat = "$ #,##0.00_);(#,##0.00)";
            cellFmt.setCustomFormat(numberFormat);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
            
            fcolor = m_view.getPaletteEntry(19).getRGB();
            bcolor = java.awt.Color.red.getRGB();
            for (int i = m_view.getSelStartRow() + 1; i < m_view.getSelEndRow() - 1; i = i + 2)
            {
                newSelection =  m_view.formatRCNr(i, m_view.getSelStartCol(), false) +
                                ":" + m_view.formatRCNr(i, m_view.getSelEndCol(), false);
                                
                m_view.setSelection(newSelection);
                SetHatchPattern(cellFmt, fcolor, bcolor);
                m_view.setCellFormat(cellFmt);
            }
    
            fcolor = java.awt.Color.white.getRGB();
            bcolor = m_view.getPaletteEntry(15).getRGB();
            for (int i = m_view.getSelStartRow() + 2; i < m_view.getSelEndRow() - 1; i = i + 2)
            {
                newSelection =  m_view.formatRCNr(i, m_view.getSelStartCol(), false) +
                                ":" + m_view.formatRCNr(i, m_view.getSelEndCol(), false);
                                
                m_view.setSelection(newSelection);
                SetHatchPattern(cellFmt, fcolor, bcolor);
                m_view.setCellFormat(cellFmt);
            }
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }    
            
    private void List2(CellFormat cellFmt)
    {
        String numberFormat, newSelection;
        int fcolor, bcolor;
        
        try{
            m_view.setSelection(savedSelection);
            //Outline Border - thin
            cellFmt.setTopBorder(CellFormat.BorderThin);
            cellFmt.setLeftBorder(CellFormat.BorderThin);
            cellFmt.setRightBorder(CellFormat.BorderThin);
            cellFmt.setTopBorderColor(m_view.getPaletteEntry(16));
            cellFmt.setLeftBorderColor(m_view.getPaletteEntry(16));
            cellFmt.setRightBorderColor(m_view.getPaletteEntry(16));
            m_view.setCellFormat(cellFmt);
        
           m_view.setSelection(hdrRange);
           cellFmt.setTopBorder(CellFormat.BorderThick);
           cellFmt.setTopBorderColor(m_view.getPaletteEntry(16));
           cellFmt.setBottomBorder(CellFormat.BorderThin);
           SetSolidPattern(cellFmt, m_view.getPaletteEntry(15).getRGB(),java.awt.Color.black.getRGB());
           AlignCenter(cellFmt);
           AdjustFont(java.awt.Color.red.getRGB(), true, true, false, cellFmt);
           m_view.setCellFormat(cellFmt);
           
           m_view.setSelection(ftrRange);
           cellFmt.setBottomBorder(CellFormat.BorderThick);
           cellFmt.setBottomBorderColor(m_view.getPaletteEntry(16));
           cellFmt.setTopBorder(CellFormat.BorderThin);
           numberFormat = "$ #,##0.00_);(#,##0.00)";
           cellFmt.setCustomFormat(numberFormat);
           AlignRight(cellFmt);
           m_view.setCellFormat(cellFmt);
           
           fcolor = java.awt.Color.red.getRGB();
           bcolor = java.awt.Color.white.getRGB();
           for (int i = m_view.getSelStartRow() + 1; i < m_view.getSelEndRow() - 1; i = i + 2)
           {
               newSelection =  m_view.formatRCNr(i, m_view.getSelStartCol(), false) +
                               ":" + m_view.formatRCNr(i, m_view.getSelEndCol(), false);
                                
               m_view.setSelection(newSelection);
               SetHatchPattern(cellFmt, fcolor, bcolor);
               m_view.setCellFormat(cellFmt);
           }
    
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
        
    
    } //List2
    
    private void List3(CellFormat cellFmt)
    {
        try{
            m_view.setSelection(colRange);
            m_view.setCellFormat(cfmt);
            m_view.setSelection(hdrRange);
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setTopBorderColor(java.awt.Color.darkGray);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorderColor(java.awt.Color.darkGray);
            AlignCenter(cellFmt);
            AdjustFont(m_view.getPaletteEntry(11).getRGB(), true, false, false, cellFmt);
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(ftrRange);
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setTopBorderColor(java.awt.Color.darkGray);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorderColor(java.awt.Color.darkGray);
            AlignRight(cellFmt);
            m_view.setCellFormat(cellFmt);
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }//List3
    
    
    private void AdjustFont(
        int  color,
        boolean         bold,
        boolean         italic,
        boolean         underline,
        CellFormat cellFmt)
    {                       
        cellFmt.setFontBold(bold);
        cellFmt.setFontItalic(italic);
        cellFmt.setFontUnderline((short) (underline?1:0));
            
        cellFmt.setFontColor(new Color(color));
    
    } //AdjustFont
    
    private void AlignCenter(CellFormat cellFmt)
    {
        try{
            cellFmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentCenter);
            cellFmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
            cellFmt.setWordWrap(false);
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }//AlignCenter            
        
    private void AlignRight(CellFormat cellFmt)
    {
        try{
            cellFmt.setHorizontalAlignment(CellFormat.HorizontalAlignmentRight);
            cellFmt.setVerticalAlignment(CellFormat.VerticalAlignmentBottom);
            cellFmt.setWordWrap(false);
            
        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    }//AlignRight
    
    private void Set3DBorder(CellFormat cellFmt,
                int row1, 
                int col1, 
                int row2, 
                int col2,
                int outlineColor,
                int rightColor,
                int bottomColor)
    {
        try{
            m_view.setSelection(row1, col1, row2, col2);
            //create an outline around the selection
            cellFmt.setTopBorder(CellFormat.BorderMedium);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            cellFmt.setLeftBorder(CellFormat.BorderMedium);
            cellFmt.setRightBorder(CellFormat.BorderMedium);
            cellFmt.setTopBorderColor(new Color(outlineColor));
            cellFmt.setBottomBorderColor(new Color(outlineColor));
            cellFmt.setLeftBorderColor(new Color(outlineColor));
            cellFmt.setRightBorderColor(new Color(outlineColor));
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(row1, col2, row2, col2);
            cellFmt.setRightBorder(CellFormat.BorderMedium);
            cellFmt.setRightBorderColor(new Color(rightColor));
            m_view.setCellFormat(cellFmt);
            
            m_view.setSelection(row2, col1, row2, col2);
            cellFmt.setBottomBorder(CellFormat.BorderMedium);
            //m_view.getPaletteEntry(bottomColor);
            cellFmt.setBottomBorderColor(new Color(bottomColor));
            m_view.setCellFormat(cellFmt);

        }
        catch (com.jxcell.CellException e){
            System.out.println(e.getMessage());
        }
    } //3DBorder
        
    private void SetSolidPattern(CellFormat cellFmt,
                 int fcolor, 
                 int bcolor)
    {
            short nPattern;
            nPattern = 1;
            try{
                cellFmt.setPattern(nPattern);
                cellFmt.setPatternFG(new Color(fcolor));
                cellFmt.setPatternBG(new Color(bcolor));
                
            }
            catch (Exception e){
                System.out.println(e.getMessage());
            }
            
    } //SetSolidPattern
    
    private void SetHatchPattern(CellFormat cellFmt,
            int fcolor,
            int bcolor)
            
    {
        try{
                cellFmt.setPattern((short)4);
                cellFmt.setPatternFG(new Color(fcolor));
                cellFmt.setPatternBG(new Color(bcolor));
        }
        catch (Exception e){
            System.out.println(e.getMessage());
        }
    }//SetHatchPattern
    
    

}



GUI部分代码:

import com.jxcell.CellFormat;
import com.jxcell.View;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;

public class Format extends JFrame implements java.awt.event.ActionListener{

    CellFormat originalCellFormat;

    BorderLayout borderLayout1 = new BorderLayout();
    JPanel jPanel1 = new JPanel();
    JButton btnSimple = new JButton();
    JButton btnClassic1 = new JButton();
    JButton btnClassic2 = new JButton();
    JButton btnClassic3 = new JButton();
    JButton btnAcct1 = new JButton();
    JButton btnAcct2 = new JButton();
    JButton btnAcct3 = new JButton();
    JButton btnColor1 = new JButton();
    JButton btnColor2 = new JButton();
    JButton btnColor3 = new JButton();
    JButton btnList1 = new JButton();
    JButton btnList2 = new JButton();
    JButton btnList3 = new JButton();
    JButton btn3DEffect1 = new JButton();

    View m_view = new View();

  //Construct the frame
  public Format() {
      enableEvents(AWTEvent.WINDOW_EVENT_MASK);
      try  {
          jbInit();
          m_view.setShowEditBar(false);
          m_view.setShowRowHeading(false);
          m_view.setShowColHeading(false);
          m_view.setShowTabs((short)0);
      }
      catch(Exception e) {
        e.printStackTrace();
      }
  }


  static public void main(String args[])
	{

 		Format frame = new Format();
    frame.validate();
    frame.setVisible(true);
	}

  //Component initialization
  private void jbInit() throws Exception  {
    this.getContentPane().setLayout(borderLayout1);
    this.setSize(new Dimension(522, 493));
    this.setTitle("Format Demo");
    btnSimple.setText("Simple");
    btnSimple.setBounds(new Rectangle(6, 3, 111, 35));
    btnSimple.addActionListener(this);

    btnClassic1.setText("Classic 1");
    btnClassic1.setBounds(new Rectangle(136, 3, 112, 35));
    btnClassic1.addActionListener(this);

    btnClassic2.setText("Classic 2");
    btnClassic2.setBounds(new Rectangle(265, 3, 113, 35));
    btnClassic2.addActionListener(this);

    btnClassic3.setPreferredSize(new Dimension(100, 27));
    btnClassic3.setText("Classic 3");
    btnClassic3.setBounds(new Rectangle(397, 2, 113, 35));
    btnClassic3.addActionListener(this);

    btnAcct1.setPreferredSize(new Dimension(111, 27));
    btnAcct1.setText("Accounting 1");
    btnAcct1.setBounds(new Rectangle(6, 45, 111, 35));
    btnAcct1.addActionListener(this);

    btnAcct2.setText("Accounting 2");
    btnAcct2.setBounds(new Rectangle(137, 45, 112, 35));
    btnAcct2.addActionListener(this);

    btnAcct3.setText("Accounting 3");
    btnAcct3.setBounds(new Rectangle(267, 44, 113, 35));
    btnAcct3.addActionListener(this);

    btnColor1.setText("Color 1");
    btnColor1.setBounds(new Rectangle(398, 47, 113, 35));
    btnColor1.addActionListener(this);

    btnColor2.setText("Color 2");
    btnColor2.setBounds(new Rectangle(6, 86, 113, 35));
    btnColor2.addActionListener(this);

    btn3DEffect1.setText("3D Effects");
    btn3DEffect1.setBounds(new Rectangle(267, 130, 112, 35));
    btn3DEffect1.addActionListener(this);

    btnColor3.setMinimumSize(new Dimension(111, 27));
    btnColor3.setPreferredSize(new Dimension(111, 27));
    btnColor3.setText("Color 3");
    btnColor3.setBounds(new Rectangle(138, 87, 111, 35));
    btnColor3.addActionListener(this);

    btnList1.setText("List 1");
    btnList1.setBounds(new Rectangle(267, 88, 113, 35));
    btnList1.addActionListener(this);

    btnList2.setText("List 2");
    btnList2.setBounds(new Rectangle(399, 92, 112, 35));
    btnList2.addActionListener(this);

    btnList3.setText("List 3");
    btnList3.setBounds(new Rectangle(138, 129, 113, 35));
    btnList3.addActionListener(this);

    jPanel1.setLayout(null);
    m_view.setBounds(new Rectangle(4, 173, 507, 293));
    this.getContentPane().add(jPanel1, BorderLayout.CENTER);

    jPanel1.add(m_view, null);
    jPanel1.add(btnSimple, null);
    jPanel1.add(btnAcct1, null);
    jPanel1.add(btnColor2, null);
    jPanel1.add(btnClassic1, null);
    jPanel1.add(btnAcct2, null);
    jPanel1.add(btnColor3, null);
    jPanel1.add(btnList3, null);
    jPanel1.add(btnClassic3, null);
    jPanel1.add(btnList1, null);
    jPanel1.add(btnClassic2, null);
    jPanel1.add(btnAcct3, null);
    jPanel1.add(btn3DEffect1, null);
    jPanel1.add(btnColor1, null);
    jPanel1.add(btnList2, null);


  }

  //Overridden so we can exit on System Close
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }

  public void actionPerformed(java.awt.event.ActionEvent e){
         Object buttonObj = e.getSource();
         if(buttonObj==btnSimple) {
           btnSimple_actionPerformed(); }
         else if(buttonObj==btnClassic1) {
           btnClassic1_actionPerformed(); }
         else if(buttonObj==btnClassic2) {
           btnClassic2_actionPerformed();}
         else if(buttonObj==btnClassic3) {
           btnClassic3_actionPerformed();}
         else if(buttonObj==btnAcct1) {
           btnAcct1_actionPerformed();}
         else if(buttonObj==btnAcct2) {
           btnAcct2_actionPerformed();}
         else if(buttonObj==btnAcct3) {
           btnAcct3_actionPerformed();}
         else if(buttonObj==btn3DEffect1) {
           btn3DEffect1_actionPerformed();}
         else if(buttonObj==btnColor1) {
           btnColor1_actionPerformed();}
         else if(buttonObj==btnColor2) {
           btnColor2_actionPerformed();}
         else if(buttonObj==btnColor3) {
           btnColor3_actionPerformed();}
         else if(buttonObj==btnList1) {
           btnList1_actionPerformed();}
         else if(buttonObj==btnList2) {
           btnList2_actionPerformed();}
         else if(buttonObj==btnList3) {
           btnList3_actionPerformed();}
  }


  public void btnSimple_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.SIMPLE);
  }

  public void btnClassic1_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.CLASSIC1);
  }

  public void btnClassic2_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.CLASSIC2);
  }

  public void btnClassic3_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.CLASSIC3);
  }

  public void btnAcct1_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.ACCOUNTING1);
  }

  public void btnAcct2_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.ACCOUNTING2);
  }

  public void btnAcct3_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.ACCOUNTING3);
  }

  public void btn3DEffect1_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.EFFECTS3D1);
  }

  public void btnColor1_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.COLORFUL1);
  }

  public void btnColor2_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.COLORFUL2);
  }

  public void btnColor3_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.COLORFUL3);
  }

  public void btnList1_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.LIST1);
  }

  public void btnList2_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.LIST2);
  }

  public void btnList3_actionPerformed() {
    CellFrmt cFrmt = new CellFrmt(m_view);
    cFrmt.FormatCells(cFrmt.LIST3);
  }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值