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

现在已经有了单元格对象,我们来读取单元格数据:

[csharp]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  1. ....  
[csharp]  view plain copy
  1. row_elem.AppendChild(cell_elem); // XML操作插入单元格到列元素  
  2.   
  3.   
  4.   
  5. releaseComObject(cell); // 释放  
  6.   
  7. cell = null;  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值