JAVA:旋转表头
以下代码对指定表头进行旋转,不知道什么用的就算了,呵呵,有测试代码,可以测试测试,性能一般,有待提高
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>");
}