ASP.NET中應用Excel:(4)格式和公式

現在已經有了單元格對象,我們來讀取單元格數據:

[csharp] view plaincopy
  1. Excel.Range cell = (Excel.Range)range.get_Item(r, c); // 獲取單元格對象  
  2.   
  3.   
  4.   
  5. if ( (bool)cell.HasFormula ) // 該單元格式爲公式  
  6.   
  7. {  
  8.   
  9.     // cell_elem爲XmlElement對象  
  10.   
  11.     cell_elem.SetAttribute("hasFormula""true");  
  12.   
  13.     cell_elem.SetAttribute("formula", cell.Formula.ToString()); // 獲取公式內容  
  14.   
  15.   cell_elem.SetAttribute("value", cell.Text.ToString()); // 注意:cell.Text爲公式的計算結果+格式設置的結果,不一定等於公式表達式  
  16.   
  17.   ....  
  18.   
  19. }  
  20.   
  21.   
  22.   
  23. // 單元格式的對齊方式  
  24.   
  25. switch ((int)cell.HorizontalAlignment) // 水平對齊方式  
  26.   
  27. {  
  28.   
  29.     case (int)Excel.Constants.xlCenter: cell_elem.SetAttribute("align""center"); break// 居中  
  30.   
  31.     case (int)Excel.Constants.xlLeft: cell_elem.SetAttribute("align""left"); break// 靠左  
  32.   
  33.     case (int)Excel.Constants.xlRight: cell_elem.SetAttribute("align""right"); break// 靠右  
  34.   
  35.     default: cell_elem.SetAttribute("align"""); break// 默認  
  36.   
  37. }  
[csharp] view plaincopy
  1. switch ((int)cell.VerticalAlignment) // 垂直對齊方式  
  2.   
  3. {  
  4.   
  5.     case (int)Excel.Constants.xlTop: cell_elem.SetAttribute("valign""top"); break// 置頂  
  6.   
  7.     case (int)Excel.Constants.xlCenter: cell_elem.SetAttribute("valign""middle"); break// 居中  
  8.   
  9.     case (int)Excel.Constants.xlBottom: cell_elem.SetAttribute("valign""bottom"); break// 置底  
  10.   
  11.     default: cell_elem.SetAttribute("valign"""); break// 默認  
  12.   
  13. }  
[csharp] view plaincopy
  1. // 字體屬性  
  2.   
  3. {  
  4.   
  5.     Excel.Font font = cell.Font; // 注意:對字體的引用會產生計數,不要忘了銷燬它  
  6.   
  7.   
  8.   
  9.     cell_elem.SetAttribute("font-family", font.Name.ToString()); // 字體名稱  
  10.   
  11.   
  12.   
  13.     if (font.Color.GetType() != typeof(System.DBNull)) // 顏色有可能爲空值  
  14.   
  15.     {  
  16.   
  17.         colorVal = (int)(double)font.Color; // 轉換一下  
  18.   
  19.         colorVal = colorVal >> 16 | colorVal & 0xFF00 | colorVal << 16; // 原來是以BGR方式,需要轉換成RGB方式  
  20.   
  21.     }  
  22.   
  23.     else  
  24.   
  25.         colorVal = 0; // 黑色  
  26.   
  27.   
  28.   
  29.     cell_elem.SetAttribute("font-color"string.Format("#{0:X2}{1:X2}{2:X2}", colorVal & 0xFF, (colorVal >> 8) & 0xFF, (colorVal >> 16) & 0xFF)); // 轉換成#RRGGBB的方式,用於HTML元素  
  30.   
  31.     cell_elem.SetAttribute("font-italic", font.Italic.ToString());  // Font.Italic類型爲布爾型  
  32.   
  33.     cell_elem.SetAttribute("font-bold", font.Bold.ToString());  // 同上  
  34.   
  35.     cell_elem.SetAttribute("font-size", font.Size.GetType() != typeof(System.DBNull) ? // 字體尺寸爲pt(point)  
  36.   
  37.                                         font.Size.ToString() + "pt" : "12pt");  
  38.   
  39.     cell_elem.SetAttribute("font-strikethrough", font.Strikethrough.ToString()); // 中心劃線  
  40.   
  41.   
  42.   
  43.     releaseComObject(font); // 釋放  
  44.   
  45.     font = null;  
  46.   
  47. }  
[csharp] view plaincopy
  1. // 處理單元格合併   
  2.   
  3. if ((bool)cell.MergeCells) // 是否爲合併的單元格?  
  4.   
  5. {  
  6.   
  7.     Excel.Range ma = (Excel.Range)cell.MergeArea; // 獲取合併區域  
  8.   
  9.     Excel.Range macol = ma.Columns; // 合併包含的行  
  10.   
  11.     Excel.Range marow = ma.Rows; // 合併包含的列  
  12.   
  13.   
  14.   
  15.     cell_elem.SetAttribute("rowspan", marow.Count.ToString()); // 行跨度  
  16.   
  17.     cell_elem.SetAttribute("colspan", macol.Count.ToString());  // 列跨度  
  18.   
  19.     cell_elem.SetAttribute("width", ma.Width.ToString() + "pt"); // 區域的尺寸  
  20.   
  21.     cell_elem.SetAttribute("height", ma.Height.ToString() + "pt");  
  22.   
  23.   
  24.   
  25.     releaseComObject(marow); marow = null// 收尾工作一定要做  
  26.   
  27.     releaseComObject(macol); macol = null;  
  28.   
  29.     releaseComObject(ma); ma = null;  
[csharp] view plaincopy
  1.     ....  
  2.   
  3. }  
  4.   
  5. else  
  6.   
  7. {   // 沒有合併  
  8.   
  9.     cell_elem.SetAttribute("colspan""1"); // 普通跨度  
  10.   
  11.     cell_elem.SetAttribute("rowspan""1");  
  12.   
  13.     cell_elem.SetAttribute("width", cell.Width.ToString() + "pt"); // 不同的尺寸  
  14.   
  15.     cell_elem.SetAttribute("height", cell.Height.ToString() + "pt");  
[csharp] view plaincopy
  1. ....  
[csharp] view plaincopy
  1. row_elem.AppendChild(cell_elem); // XML操作插入單元格到列元素  
  2.   
  3.   
  4.   
  5. releaseComObject(cell); // 釋放  
  6.   
  7. cell = null;  

 

 

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