現在已經有了單元格對象,我們來讀取單元格數據:
- Excel.Range cell = (Excel.Range)range.get_Item(r, c); // 獲取單元格對象
- if ( (bool)cell.HasFormula ) // 該單元格式爲公式
- {
- // cell_elem爲XmlElement對象
- cell_elem.SetAttribute("hasFormula", "true");
- cell_elem.SetAttribute("formula", cell.Formula.ToString()); // 獲取公式內容
- cell_elem.SetAttribute("value", cell.Text.ToString()); // 注意:cell.Text爲公式的計算結果+格式設置的結果,不一定等於公式表達式
- ....
- }
- // 單元格式的對齊方式
- switch ((int)cell.HorizontalAlignment) // 水平對齊方式
- {
- case (int)Excel.Constants.xlCenter: cell_elem.SetAttribute("align", "center"); break; // 居中
- case (int)Excel.Constants.xlLeft: cell_elem.SetAttribute("align", "left"); break; // 靠左
- case (int)Excel.Constants.xlRight: cell_elem.SetAttribute("align", "right"); break; // 靠右
- default: cell_elem.SetAttribute("align", ""); break; // 默認
- }
- switch ((int)cell.VerticalAlignment) // 垂直對齊方式
- {
- case (int)Excel.Constants.xlTop: cell_elem.SetAttribute("valign", "top"); break; // 置頂
- case (int)Excel.Constants.xlCenter: cell_elem.SetAttribute("valign", "middle"); break; // 居中
- case (int)Excel.Constants.xlBottom: cell_elem.SetAttribute("valign", "bottom"); break; // 置底
- default: cell_elem.SetAttribute("valign", ""); break; // 默認
- }
- // 字體屬性
- {
- Excel.Font font = cell.Font; // 注意:對字體的引用會產生計數,不要忘了銷燬它
- cell_elem.SetAttribute("font-family", font.Name.ToString()); // 字體名稱
- if (font.Color.GetType() != typeof(System.DBNull)) // 顏色有可能爲空值
- {
- colorVal = (int)(double)font.Color; // 轉換一下
- colorVal = colorVal >> 16 | colorVal & 0xFF00 | colorVal << 16; // 原來是以BGR方式,需要轉換成RGB方式
- }
- else
- colorVal = 0; // 黑色
- cell_elem.SetAttribute("font-color", string.Format("#{0:X2}{1:X2}{2:X2}", colorVal & 0xFF, (colorVal >> 8) & 0xFF, (colorVal >> 16) & 0xFF)); // 轉換成#RRGGBB的方式,用於HTML元素
- cell_elem.SetAttribute("font-italic", font.Italic.ToString()); // Font.Italic類型爲布爾型
- cell_elem.SetAttribute("font-bold", font.Bold.ToString()); // 同上
- cell_elem.SetAttribute("font-size", font.Size.GetType() != typeof(System.DBNull) ? // 字體尺寸爲pt(point)
- font.Size.ToString() + "pt" : "12pt");
- cell_elem.SetAttribute("font-strikethrough", font.Strikethrough.ToString()); // 中心劃線
- releaseComObject(font); // 釋放
- font = null;
- }
- // 處理單元格合併
- if ((bool)cell.MergeCells) // 是否爲合併的單元格?
- {
- Excel.Range ma = (Excel.Range)cell.MergeArea; // 獲取合併區域
- Excel.Range macol = ma.Columns; // 合併包含的行
- Excel.Range marow = ma.Rows; // 合併包含的列
- cell_elem.SetAttribute("rowspan", marow.Count.ToString()); // 行跨度
- cell_elem.SetAttribute("colspan", macol.Count.ToString()); // 列跨度
- cell_elem.SetAttribute("width", ma.Width.ToString() + "pt"); // 區域的尺寸
- cell_elem.SetAttribute("height", ma.Height.ToString() + "pt");
- releaseComObject(marow); marow = null; // 收尾工作一定要做
- releaseComObject(macol); macol = null;
- releaseComObject(ma); ma = null;
- ....
- }
- 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");
- ....
- row_elem.AppendChild(cell_elem); // XML操作插入單元格到列元素
- releaseComObject(cell); // 釋放
- cell = null;