JAVA:旋轉表頭

來源:互聯網
上載者:User

以下代碼對指定表頭進行旋轉,不知道什麼用的就算了,呵呵,有測試代碼,可以測試測試,效能一般,有待提高 

static class HeaderCell
    {
        public int RowSpan = 0,ColSpan = 0;
        public String Text = null;
        public String Title = null;
    }
    static class VirtualHeaderTitles
    {
        public String[] Headers=null;
        public boolean isSum =false;
    }
    public TestHeader()
    {
    }
    private static Vector getHeader(Vector vect)
    {
        Vector vectret =new Vector();
        int nPrev=1;
        int nLevel =1;
        for(int i =vect.size()-1;i>=0;i--)
        {
            System.out.println("----"+i);
            Vector vectH =new Vector();
            VirtualHeaderTitles h = (VirtualHeaderTitles)vect.elementAt(i);
            boolean bSum = h.isSum&&h.Headers.length>1;
            String strSumTitle="";
            for(int x =0;x<h.Headers.length;x++)
            {
                System.out.println("----"+i+"   "+x);

                HeaderCell xxx = new HeaderCell();
                xxx.ColSpan = nPrev;
                xxx.Text = h.Headers[x];
                strSumTitle+=" "+ xxx.Text;
                vectH.add(xxx);
               
            }
            if(bSum )
             {
                    HeaderCell sumH = new HeaderCell();
                    sumH.Text = "小計";
                    sumH.RowSpan = nLevel;
                    sumH.Title =" 對 "+strSumTitle+" 小計";
                    vectH.add(sumH);               
              }

            nPrev=h.Headers.length*nPrev+
                (bSum ?1:0);
            vectret.add(vectH);
            nLevel++;
        }
        return vectret;
    }
    public static void main(String[] args)
    {
        VirtualHeaderTitles v1=new VirtualHeaderTitles();
        v1.Headers =new String[]{"A","B"};
        v1.isSum=true;
       
        VirtualHeaderTitles v2 =new VirtualHeaderTitles();
        v2.Headers=new String[]{"aZone","bZone"};
        v2.isSum=true;
       
        VirtualHeaderTitles v3 =new VirtualHeaderTitles();
        v3.Headers=new String[]{"Test1","Test2"};
        v3.isSum=true;
       
        VirtualHeaderTitles v4 =new VirtualHeaderTitles();
        v4.Headers=new String[]{"Measure1","Measure2","Measure3"};
        v4.isSum=true;

       
       
        Vector vH =new Vector();
        vH.add(v1);
        vH.add(v2);
        vH.add(v3);
        vH.add(v4);
        Vector formatedVector = getHeader(vH);
        System.out.println("<table border=1>");
        int nInitRowIndex =0;
        int nNextMultiCount =1;
        int nAllCellsCount =0;
        for(int nTableLineR = formatedVector.size() - 1;nTableLineR >= 0;nTableLineR--)
        {
            int nCurrentMultiCount=nNextMultiCount;
            nNextMultiCount =nNextMultiCount *((VirtualHeaderTitles)vH.elementAt(nInitRowIndex)).Headers.length;
            System.out.println("<tr ROW="+nInitRowIndex+">");
            Vector vectCells = (Vector) formatedVector.elementAt(nTableLineR);
            for(int nMultiIndex=0;nMultiIndex<nCurrentMultiCount;nMultiIndex++)
            {
                System.out.print("\r\n");
                for(int nCellIndex = 0;nCellIndex < vectCells.size();nCellIndex++)
                {
                    HeaderCell cell = (HeaderCell) vectCells.elementAt(nCellIndex);
                    System.out.print("<td nowrap align=center bgcolor=yellow ");
                    if(null!=cell.Title)
                    {
                        System.out.print(" title=\""+cell.Title+"\" ");
                    }
                    if(cell.ColSpan > 1)
                    {
                        System.out.print("colspan=" + cell.ColSpan + " ");
                    }
                    if(cell.RowSpan > 1)
                    {
                        System.out.print("rowspan=" + cell.RowSpan + " ");
                    }
                    System.out.print(">" + cell.Text + "</td>\r\n");
                    if(nInitRowIndex == 0)
                    {
                        nAllCellsCount +=(cell.ColSpan>1)?cell.ColSpan:1;
                    }

                }
            }
            System.out.println("</tr>");
           
            nInitRowIndex++;
           
        }
        System.out.println("<tr>");
        for(int i =0;i<nAllCellsCount;i++)
        {
            System.out.println("<td>?</td>");
        }
        System.out.println("</tr>");
        System.out.println("</table>");
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.