ASP.NETにExcelを適用:(7)表サイズの計算


前節では、表のサイズをどのように取得するかは説明していません.表サイズの計算は、Excelワークシートのデータを読み込むときに行うべきで、データを含むセルの最大列番号と行番号の計算も行います.
具体的な実装は次のとおりです.
for (int r = rowRange.Count; r >= 1; r--)

{

    XmlElement row_elem = xml.CreateElement("ROW");



    double rowHeight = 18.5f;



    for (int c = 1; c < colRange.Count; c++)

    {

        if (data[r, c] != null)

        {

            ....

            //      

            cell_elem.SetAttribute("row", r.ToString());

            cell_elem.SetAttribute("col", c.ToString());



            //              

            if ((bool)cell.MergeCells)

            {

                Excel.Range ma = (Excel.Range)cell.MergeArea;

                Excel.Range macol = ma.Columns;

                Excel.Range marow = ma.Rows;



                //       

                ...

                //          

                cell_elem.SetAttribute("width", ma.Width.ToString() + "pt");

                cell_elem.SetAttribute("height", ma.Height.ToString() + "pt");



                //          

                if ((double)ma.Height > rowHeight) rowHeight = (double)ma.Height;

                

                //   

                ...

            }

            else

            {

                //       

                cell_elem.SetAttribute("colspan", "1");

                cell_elem.SetAttribute("rowspan", "1");

                cell_elem.SetAttribute("width", cell.Width.ToString() + "pt");

                cell_elem.SetAttribute("height", cell.Height.ToString() + "pt");



                if ((double)cell.Height > rowHeight) rowHeight = (double)cell.Height;

            }



            row_elem.AppendChild(cell_elem);



            if (r > maxRow) maxRow = r; //       



            if (c > maxCol) maxCol = c; //       



            //   

            ....

        } // if has data

    } // for c



    if (row_elem.ChildNodes.Count > 0 || sheet_elem.ChildNodes.Count > 0)

    {

        row_elem.SetAttribute("height", rowHeight.ToString() + "pt"); //       

        sheet_elem.InsertBefore(row_elem, sheet_elem.FirstChild);

    }

} // for r



sheet_elem.SetAttribute("row", maxRow.ToString()); //        ,              

sheet_elem.SetAttribute("col", maxCol.ToString());



{   //                     

    Excel.Range borderCell = (Excel.Range)range.get_Item(maxRow, maxCol); //          



     //              ,  :                ,         

    sheet_elem.SetAttribute("width", ((double)borderCell.Left + (double)borderCell.Width).ToString());

    sheet_elem.SetAttribute("height", ((double)borderCell.Top + (double)borderCell.Height).ToString());

    

    releaseComObject(borderCell); //     

    borderCell = null;

}

ワークシートのサイズがあれば、TabPanelのサイズを設定できます.
panel.ID = "Panel" + m;



//     TabPanel   

panel.Attributes.Add("Width", sheet_node_list[m].Attributes["width"].Value);

panel.Attributes.Add("Height", sheet_node_list[m].Attributes["height"].Value);
//              



int _rowNum = Math.Max(1, int.Parse(sheetNodes[m].Attributes["row"].Value) + 1);

int _colNum = Math.Max(1, int.Parse(sheetNodes[m].Attributes["col"].Value) + 1);

表にセルとデータを入力するときに、表のサイズを設定します.
protected void fillTableData(Table tbl, int _rowNum, int _colNum, XmlNode  sheet_node)

{

    tbl.Attributes.Add("cellpadding", "0");

    tbl.Attributes.Add("cellspacing", "0");



    tbl.Style.Add("border-collapse", "collapse");

    tbl.Style.Add("table-layout", "fixed");

    //        ?       

    tbl.Style.Add("width", sheet_node.Attributes["width"].Value + "pt");

    tbl.Style.Add("border", "1px solid black");

    ....

TabContainerのプロパティも設定する必要があります.プロパティパネルでWidthとHeightをクリアし、ScrolBarsを「Auto」に設定します.これにより、比較的完璧なインタフェースが表示されます.そうでない場合は、コードをよくチェックし、他の異なる属性値を試してください.