在ASP.NET中應用Excel:(7)表格尺寸的計算

上一節留了個尾巴,沒有說如何獲取表格的尺寸。其實表格尺寸的計算在讀取Excel工作表數據時就應該進行,同時進行的還有計算包含數據的單元格的最大列號和行號。

具體實現是:

[csharp] view plaincopy
  1. for (int r = rowRange.Count; r >= 1; r--)  
  2.   
  3. {  
  4.   
  5.     XmlElement row_elem = xml.CreateElement("ROW");  
  6.   
  7.   
  8.   
  9.     double rowHeight = 18.5f;  
  10.   
  11.   
  12.   
  13.     for (int c = 1; c < colRange.Count; c++)  
  14.   
  15.     {  
  16.   
  17.         if (data[r, c] != null)  
  18.   
  19.         {  
  20.   
  21.             ....  
  22.   
  23.             // 獲取行列號  
  24.   
  25.             cell_elem.SetAttribute("row", r.ToString());  
  26.   
  27.             cell_elem.SetAttribute("col", c.ToString());  
  28.   
  29.   
  30.   
  31.             // 計算合併和普通單元格的尺寸  
  32.   
  33.             if ((bool)cell.MergeCells)  
  34.   
  35.             {  
  36.   
  37.                 Excel.Range ma = (Excel.Range)cell.MergeArea;  
  38.   
  39.                 Excel.Range macol = ma.Columns;  
  40.   
  41.                 Excel.Range marow = ma.Rows;  
  42.   
  43.   
  44.   
  45.                 // 獲取合併屬性  
  46.   
  47.                 ...  
  48.   
  49.                 // 獲取合併區域的尺寸  
  50.   
  51.                 cell_elem.SetAttribute("width", ma.Width.ToString() + "pt");  
  52.   
  53.                 cell_elem.SetAttribute("height", ma.Height.ToString() + "pt");  
  54.   
  55.   
  56.   
  57.                 // 更新本行的最大高度  
  58.   
  59.                 if ((double)ma.Height > rowHeight) rowHeight = (double)ma.Height;  
  60.   
  61.                   
  62.   
  63.                 // 釋放  
  64.   
  65.                 ...  
  66.   
  67.             }  
  68.   
  69.             else  
  70.   
  71.             {  
  72.   
  73.                 // 非合併單元格  
  74.   
  75.                 cell_elem.SetAttribute("colspan""1");  
  76.   
  77.                 cell_elem.SetAttribute("rowspan""1");  
  78.   
  79.                 cell_elem.SetAttribute("width", cell.Width.ToString() + "pt");  
  80.   
  81.                 cell_elem.SetAttribute("height", cell.Height.ToString() + "pt");  
  82.   
  83.   
  84.   
  85.                 if ((double)cell.Height > rowHeight) rowHeight = (double)cell.Height;  
  86.   
  87.             }  
  88.   
  89.   
  90.   
  91.             row_elem.AppendChild(cell_elem);  
  92.   
  93.   
  94.   
  95.             if (r > maxRow) maxRow = r; // 更新最大行號  
  96.   
  97.   
  98.   
  99.             if (c > maxCol) maxCol = c; // 更新最大列號  
  100.   
  101.   
  102.   
  103.             // 釋放  
  104.   
  105.             ....  
  106.   
  107.         } // if has data  
  108.   
  109.     } // for c  
  110.   
  111.   
  112.   
  113.     if (row_elem.ChildNodes.Count > 0 || sheet_elem.ChildNodes.Count > 0)  
  114.   
  115.     {  
  116.   
  117.         row_elem.SetAttribute("height", rowHeight.ToString() + "pt"); // 這裏設置行高  
  118.   
  119.         sheet_elem.InsertBefore(row_elem, sheet_elem.FirstChild);  
  120.   
  121.     }  
  122.   
  123. // for r  
  124.   
  125.   
  126.   
  127. sheet_elem.SetAttribute("row", maxRow.ToString()); // 保存最大行列號,也就是工作表實際有數據的區域  
  128.   
  129. sheet_elem.SetAttribute("col", maxCol.ToString());  
  130.   
  131.   
  132.   
  133. {   // 這段代碼描述如何獲取工作表指定範圍的尺寸  
  134.   
  135.     Excel.Range borderCell = (Excel.Range)range.get_Item(maxRow, maxCol); // 獲取最右下的單元格  
  136.   
  137.   
  138.   
  139.      // 獲取其尺寸作爲工作表的尺寸,注意:未考慮如果該單元格是否合併的問題,留待同學的課後作業  
  140.   
  141.     sheet_elem.SetAttribute("width", ((double)borderCell.Left + (double)borderCell.Width).ToString());  
  142.   
  143.     sheet_elem.SetAttribute("height", ((double)borderCell.Top + (double)borderCell.Height).ToString());  
  144.   
  145.       
  146.   
  147.     releaseComObject(borderCell); // 收尾工作  
  148.   
  149.     borderCell = null;  
  150.   
  151. }  

有了工作表的尺寸,就可以設置TabPanel的尺寸了:

[csharp] view plaincopy
  1. panel.ID = "Panel" + m;  
  2.   
  3.   
  4.   
  5. // 這裏設置TabPanel的尺寸  
  6.   
  7. panel.Attributes.Add("Width", sheet_node_list[m].Attributes["width"].Value);  
  8.   
  9. panel.Attributes.Add("Height", sheet_node_list[m].Attributes["height"].Value);  
[csharp] view plaincopy
  1. // 這裏獲取工作表的最大行列號  
  2.   
  3.   
  4.   
  5. int _rowNum = Math.Max(1, int.Parse(sheetNodes[m].Attributes["row"].Value) + 1);  
  6.   
  7. int _colNum = Math.Max(1, int.Parse(sheetNodes[m].Attributes["col"].Value) + 1);  

在向表格填充單元格和數據的時候,設置表格的尺寸:

[csharp] view plaincopy
  1. protected void fillTableData(Table tbl, int _rowNum, int _colNum, XmlNode  sheet_node)  
  2.   
  3. {  
  4.   
  5.     tbl.Attributes.Add("cellpadding""0");  
  6.   
  7.     tbl.Attributes.Add("cellspacing""0");  
  8.   
  9.   
  10.   
  11.     tbl.Style.Add("border-collapse""collapse");  
  12.   
  13.     tbl.Style.Add("table-layout""fixed");  
  14.   
  15.     // 還記得這一行嗎?設置表格的寬度  
  16.   
  17.     tbl.Style.Add("width", sheet_node.Attributes["width"].Value + "pt");  
  18.   
  19.     tbl.Style.Add("border""1px solid black");  
  20.   
  21.     ....  

還需要設置TabContainer的屬性: 在屬性面板中,清空Width和Height,設置ScrollBars爲"Auto"。 這樣就可以看到比較完美的界面了,如果不對,請仔細檢察代碼,並嘗試其它不同的屬性值。  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章