Applying excel in ASP. NET: (7) Calculation of table size

Source: Internet
Author: User

The previous section left a tail and did not show how to obtain the table size. In fact, the calculation of table size should be carried out when reading the Excel worksheet data, and the maximum column number and row number of cells containing data should also be calculated.

The specific implementation is:

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 ){.... // obtain the row and column number cell_elem.SetAttribute ("row", r. toString (); cell_elem.SetAttribute ("col", c. toString (); // calculates the size of the merged and common cells. if (bool) cell. mergeCells) {Excel. range ma = (Excel. range) cell. mergeArea; Excel. range macol = ma. columns; Excel. range marow = ma. rows; // obtain the merged attributes... // obtain the size of the merging area cell_elem.SetAttribute ("width", ma. width. toString () + "pt"); cell_elem.SetAttribute ("height", ma. height. toString () + "pt"); // update the maximum if (double) ma. height> rowHeight) rowHeight = (double) ma. height; // release ...} else {// unmerged cell 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; // update the maximum row if (c> maxCol) maxCol = c; // update the maximum column number // release ....} // if has data} // for c if (row_elem.ChildNodes.Count> 0 | sheet_elem.ChildNodes.Count> 0) {row_elem.SetAttribute ("height", rowHeight. toString () + "pt"); // set the row Height (row_elem, sheet_elem.InsertBefore);} // for rsheet_elem.SetAttribute ("row", maxRow. toString (); // Save the largest row and column number, that is, the area where the worksheet actually has data, sheet_elem.SetAttribute ("col", maxCol. toString (); {// This Code describes how to obtain the Excel worksheet within a specified range. range borderCell = (Excel. range) range. get_Item (maxRow, maxCol); // obtain the bottom right cell // obtain its size as the worksheet size. Note: if the cell is not merged, students waiting for homework sheet_elem.SetAttribute ("width", (double) borderCell. left + (double) borderCell. width ). toString (); sheet_elem.SetAttribute ("height", (double) borderCell. top + (double) borderCell. height ). toString (); releaseComObject (borderCell); // end work borderCell = null ;}

With the worksheet size, you can set the size of tabpanel:

Panel. id = "Panel" + m; // set the size of the tabpanel. attributes. add ("width", sheet_node_list [M]. attributes ["width"]. value); panel. attributes. add ("height", sheet_node_list [M]. attributes ["height"]. value );
// Obtain the maximum row and column number of the worksheet, 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 );

When filling cells and data in a table, set the table size:

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"); // do you still remember this row? Set the table width to TBL. style. add ("width", sheet_node.attributes ["width"]. value + "PT"); TBL. style. add ("border", "1px solid black ");....

You also need to set the attributes of tabcontainer:
In the attribute panel, clear width and height and set scrollbars to "Auto ".

In this way, you can see the perfect interface. If not, check the code carefully and try other different attribute values.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.