Excel操作类C#版2

来源:互联网 发布:淘宝买瓷砖靠谱吗 编辑:程序博客网 时间:2024/04/30 09:18
 
  1. using System;
  2. using System.Collections;
  3. using Excel = Microsoft.Office.Interop.Excel;
  4. namespace ExcelEdit
  5. {
  6.     class ExcelEdit2
  7.     {
  8.         /// 
  9.         /// 对Excel进行操作的类。
  10.         /// 
  11.         public class JointExcel
  12.         {
  13.             #region 私有成员
  14.             private Excel.ApplicationClass m_objExcel;//Excel应用程序对象
  15.             private Excel.Workbooks m_objBooks;//Excel的Books对象
  16.             private Excel.Workbook m_objBook;//当前Book对象
  17.             private Excel.Worksheet m_objSheet;//当前Sheet对象
  18.             private Excel.Range m_Range;//当前Range对象
  19.             private System.Reflection.Missing miss =
  20.             System.Reflection.Missing.Value;//空数据变量
  21.             private Excel.Font m_Font;//当前单元格的字体属性对象
  22.             private Excel.Borders m_Borders;//当前单元格或者区域的边框属性对象
  23.             //单元格的四条边框对象
  24.             private Excel.Border m_BorderTop;
  25.             private Excel.Border m_BorderBottom;
  26.             private Excel.Border m_BorderLeft;
  27.             private Excel.Border m_BorderRight;
  28.             private Excel.Range m_cellRange;//单元格Range对象,用来取得对象的Rows和Columns属性对象
  29.             //单元格列号数组
  30.             private string[] m_colString = new string[26] { "A""B""C""D""E""F""G""H""I""J""K""L""M""N""O""P""Q""R""S""T""U""V""W""X""Y""Z" };
  31.             #endregion
  32.             /// 
  33.             /// 本类使用在web application中时,请在Web.Config中添加
  34.             /// 
  35.             /// 
  36.             public JointExcel()
  37.             {
  38.                 m_objExcel = new Excel.ApplicationClass();
  39.                 m_objBooks = (Excel.Workbooks)m_objExcel.Workbooks;
  40.                 m_objBook = (Excel.Workbook)(m_objBooks.Add(miss));
  41.                 m_objSheet = (Excel.Worksheet)m_objBook.ActiveSheet;
  42.             }
  43.             ~JointExcel()
  44.             {
  45.                 //释放所有Com对象
  46.                 if (m_cellRange != null)
  47.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_cellRange);
  48.                 if (m_BorderTop != null)
  49.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderTop);
  50.                 if (m_BorderBottom != null)
  51.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderBottom);
  52.                 if (m_BorderLeft != null)
  53.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderLeft);
  54.                 if (m_BorderRight != null)
  55.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_BorderRight);
  56.                 if (m_Borders != null)
  57.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Borders);
  58.                 if (m_Font != null)
  59.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Font);
  60.                 if (m_Range != null)
  61.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Range);
  62.                 if (m_objSheet != null)
  63.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objSheet);
  64.                 if (m_objBook != null)
  65.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
  66.                 if (m_objBooks != null)
  67.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBooks);
  68.                 if (m_objExcel != null)
  69.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
  70.                 GC.Collect();
  71.             }
  72.             #region 选定单元格
  73.             private string GetCell(int ColNum, int RowNum)
  74.             {
  75.                 string temp = "A";
  76.                 int row = RowNum + 1;
  77.                 if (ColNum < 0 || ColNum > 255)
  78.                 {
  79.                     throw new Exception("行号错误");
  80.                 }
  81.                 int i0, i1 = 0;
  82.                 i0 = Math.DivRem(ColNum, 25, out i1);
  83.                 if (i0 == 0 && i1 == 0)
  84.                 {
  85.                     return "A" + row.ToString();
  86.                 }
  87.                 if (i0 == 0 && i1 > 0)
  88.                 {
  89.                     return m_colString[i1] + row.ToString();
  90.                 }
  91.                 else
  92.                 {
  93.                     //return temp + m_colString[i0] + row.ToString();
  94.                     return m_colString[i0] + m_colString[i1] + row.ToString();
  95.                 }
  96.             }
  97.             /// 
  98.             /// 选定相应单元格
  99.             /// 
  100.             /// int 列号
  101.             /// int 行号
  102.             public void SetRange(int ColNum, int RowNum)
  103.             {
  104.                 m_Range = m_objSheet.get_Range((object)GetCell(ColNum, RowNum), miss);
  105.                 m_Font = m_Range.Font;
  106.                 m_Borders = m_Range.Borders;
  107.                 m_BorderTop = m_Borders[Excel.XlBordersIndex.xlEdgeTop];
  108.                 m_BorderBottom = m_Borders[Excel.XlBordersIndex.xlEdgeBottom];
  109.                 m_BorderLeft = m_Borders[Excel.XlBordersIndex.xlEdgeLeft];
  110.                 m_BorderRight = m_Borders[Excel.XlBordersIndex.xlEdgeRight];
  111.                 m_cellRange = m_Range;
  112.             }
  113.             /// 
  114.             /// 选择相应的区域
  115.             /// 
  116.             /// 起始单元格列号
  117.             /// 起始单元格行号
  118.             /// 结束单元格列号
  119.             /// 结束单元格行号
  120.             public void SetRange(int startColNum, int startRowNum, int endColNum, int
  121.           endRowNum)
  122.             {
  123.                 m_Range =
  124.              m_objSheet.get_Range((object)GetCell(startColNum, startRowNum), (object)GetCell(endColNum, endRowNum));
  125.                 m_Font = m_Range.Font;
  126.                 m_Borders = m_Range.Borders;
  127.                 m_BorderTop = m_Borders[Excel.XlBordersIndex.xlEdgeTop];
  128.                 m_BorderBottom = m_Borders[Excel.XlBordersIndex.xlEdgeBottom];
  129.                 m_BorderLeft = m_Borders[Excel.XlBordersIndex.xlEdgeLeft];
  130.                 m_BorderRight = m_Borders[Excel.XlBordersIndex.xlEdgeRight];
  131.                 m_cellRange = m_Range;
  132.             }
  133.             #endregion
  134.             //开始具体的Excel操作
  135.             #region 给单元格附值
  136.             /// 
  137.             /// 给选定单元格附值
  138.             /// 
  139.             /// 值
  140.             public void SetCellValue(string value)
  141.             {
  142.                 if (m_Range == nullthrow new System.Exception("没有设定单元格或者区域");
  143.                 m_Range.Value2 = value;
  144.             }
  145.             /// 
  146.             /// 给选定单元格附值
  147.             /// 
  148.             /// 列号
  149.             /// 行号
  150.             /// 值
  151.             public void SetCellValue(int row, int col, string value)
  152.             {
  153.                 SetRange(col, row);
  154.                 m_Range.Value2 = value;
  155.                 m_Range.Font.Name = "Arial";
  156.                 m_Range.Font.Size = 9;
  157.             }
  158.             /// 
  159.             /// 合并选定区域后给其附值
  160.             /// 
  161.             /// 起始行号
  162.             /// 起始列号
  163.             /// 结束行号
  164.             /// 结束列号
  165.             /// 值
  166.             public void SetCellValue(int startRow, int startCol, int endRow, int
  167.           endCol, string value)
  168.             {
  169.                 Merge(startRow, startCol, endRow, endCol);
  170.                 m_Range.Value2 = value;
  171.                 m_Range.Font.Size = 9;
  172.                 m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
  173.             }
  174.             #endregion
  175.             public void SetCellbolk(int row, int col)
  176.             {
  177.                 SetRange(col, row);
  178.                 m_Range.Font.Bold = true;
  179.             }
  180.             #region 设定单元格对齐方式
  181.             /// 
  182.             /// 设定单元格中文字的对齐方式
  183.             /// 
  184.             /// 对齐方式
  185.             //public void SetHorizontal(JointEmun.ExcelAlign ea)
  186.             //{
  187.             //    if (m_Range == null) throw new System.Exception("没有设定单元格或者区域");
  188.             //    switch (ea.ToString())
  189.             //    {
  190.             //        case "Left":
  191.             //            m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  192.             //            break;
  193.             //        case "Right":
  194.             //            m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
  195.             //            break;
  196.             //        case "center":
  197.             //            m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  198.             //            break;
  199.             //        default:
  200.             //            m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  201.             //            break;
  202.             //    }
  203.             //}
  204.             /// 
  205.             /// 设定单元格中文字的对齐方式
  206.             /// 
  207.             /// 单元格行号
  208.             /// 单元格列号
  209.             /// 对齐方式
  210.             //  public void SetHorizontal(int rowIndex, int columnIndex, JointEmun.ExcelAlign
  211.             //ea)
  212.             //  {
  213.             //      SetRange(columnIndex, rowIndex);
  214.             //      switch (ea.ToString())
  215.             //      {
  216.             //          case "Left":
  217.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  218.             //              break;
  219.             //          case "Right":
  220.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
  221.             //              break;
  222.             //          case "center":
  223.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  224.             //              break;
  225.             //          default:
  226.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  227.             //              break;
  228.             //      }
  229.             //  }
  230.             /// 
  231.             /// 设定选定区域的对齐方式
  232.             /// 
  233.             /// 起始行号
  234.             /// 起始列号
  235.             /// 结束行号
  236.             /// 结束列号
  237.             /// 对齐方式
  238.             //  public void SetHorizontal(int startRowIndex, int startColumnIndex, int
  239.             //endRowIndex, int endColumnIndex, JointEmun.ExcelAlign ea)
  240.             //  {
  241.             //      SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
  242.             //      switch (ea.ToString())
  243.             //      {
  244.             //          case "Left":
  245.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  246.             //              break;
  247.             //          case "Right":
  248.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
  249.             //              break;
  250.             //          case "center":
  251.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
  252.             //              break;
  253.             //          default:
  254.             //              m_Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
  255.             //              break;
  256.             //      }
  257.             //  }
  258.             #endregion
  259.             #region 设置行高和列宽
  260.             /// 
  261.             /// 设置列宽
  262.             /// 
  263.             /// 列宽度
  264.             public void SetColumnWidth(float columnWidth)
  265.             {
  266.                 m_Range.ColumnWidth = columnWidth;
  267.             }
  268.             /// 
  269.             /// 设置列宽
  270.             /// 
  271.             /// 列号
  272.             /// 列宽度
  273.             public void SetColumnWidth(int columnIndex, float columnWidth)
  274.             {
  275.                 SetRange(columnIndex, 0);
  276.                 m_Range.ColumnWidth = columnWidth;
  277.             }
  278.             /// 
  279.             /// 设置行高
  280.             /// 
  281.             /// 行宽度
  282.             public void SetRowHeigh(float rowHeigh)
  283.             {
  284.                 m_Range.RowHeight = rowHeigh;
  285.             }
  286.             /// 
  287.             /// 设置行高
  288.             /// 
  289.             /// 行号
  290.             /// 行宽度
  291.             public void SetRowHeigh(int rowIndex, float rowHeigh)
  292.             {
  293.                 SetRange(0, rowIndex);
  294.                 m_Range.RowHeight = rowHeigh;
  295.             }
  296.             #endregion
  297.             #region 合并单元格
  298.             /// 
  299.             /// 将选定区域中的单元格合并
  300.             /// 
  301.             public void Merge()
  302.             {
  303.                 m_Range.Merge(null);
  304.             }
  305.             /// 
  306.             /// 将选定区域中的单元格合并
  307.             /// 
  308.             /// 起始行号
  309.             /// 起始列号
  310.             /// 结束行号
  311.             /// 结束列号
  312.             public void Merge(int startRowIndex, int startColumnIndex, int endRowIndex,
  313.           int endColumnIndex)
  314.             {
  315.                 SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
  316.                 m_Range.Merge(null);
  317.             }
  318.             #endregion
  319.             #region 设置字体名称、大小
  320.             /// 
  321.             /// 设置区域内的字体
  322.             /// 
  323.             /// 起始行号
  324.             /// 起始列号
  325.             /// 结束行号
  326.             /// 结束列号
  327.             /// 字体名称
  328.             public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
  329.           int endColumnIndex, string fontName)
  330.             {
  331.                 SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
  332.                 m_Font.Name = fontName;
  333.             }
  334.             /// 
  335.             /// 设置区域内的字号(文字大小)
  336.             /// 
  337.             /// 起始行号
  338.             /// 起始列号
  339.             /// 结束行号
  340.             /// 结束列号
  341.             /// 字号
  342.             public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
  343.           int endColumnIndex, int fontSize)
  344.             {
  345.                 SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
  346.                 m_Font.Size = fontSize;
  347.             }
  348.             /// 
  349.             /// 设置区域内的字体以及字号
  350.             /// 
  351.             /// 起始行号
  352.             /// 起始列号
  353.             /// 结束行号
  354.             /// 结束列号
  355.             /// 字体名称
  356.             /// 字号
  357.             public void SetFont(int startRowIndex, int startColumnIndex, int endRowIndex,
  358.           int endColumnIndex, string fontName, int fontSize)
  359.             {
  360.                 SetRange(startColumnIndex, startRowIndex, endColumnIndex, endRowIndex);
  361.                 m_Font.Name = fontName;
  362.                 m_Font.Size = fontSize;
  363.             }
  364.             /// 
  365.             /// 设置单元格的字体和字号
  366.             /// 
  367.             /// 行号
  368.             /// 列号
  369.             /// 字体
  370.             /// 字号
  371.             public void SetFont(int rowIndex, int columnIndex, string fontName, int fontSize)
  372.             {
  373.                 SetRange(columnIndex, rowIndex);
  374.                 m_Font.Name = fontName;
  375.                 m_Font.Size = fontSize;
  376.             }
  377.             /// 
  378.             /// 设置单元格的字体
  379.             /// 
  380.             /// 行号
  381.             /// 列号
  382.             /// 字体
  383.             public void SetFont(int rowIndex, int columnIndex, string fontName)
  384.             {
  385.                 SetRange(columnIndex, rowIndex);
  386.                 m_Font.Name = fontName;
  387.             }
  388.             /// 设置单元格的字号
  389.             /// 行号
  390.             /// 列号
  391.             /// 字号
  392.             public void SetFont(int rowIndex, int columnIndex, int fontSize)
  393.             {
  394.                 SetRange(columnIndex, rowIndex);
  395.                 m_Font.Size = fontSize;
  396.             }
  397.             /// 
  398.             /// 设定字体
  399.             /// 
  400.             /// 字体
  401.             public void SetFont(string fontName)
  402.             {
  403.                 m_Font.Name = fontName;
  404.             }
  405.             #endregion
  406.             public void setcolor(int rowSum, int colSum, int endrowSum, int endcolIndex, int color)
  407.             {
  408.                 m_objSheet.get_Range(m_objExcel.Cells[rowSum, colSum], m_objExcel.Cells[endrowSum, endcolIndex]).Select();
  409.                 m_objSheet.get_Range(m_objExcel.Cells[rowSum, colSum], m_objExcel.Cells[endrowSum, endcolIndex]).Interior.ColorIndex = color;//设置为浅黄色,共
  410.             }
  411.             //画边框
  412.             public void setline(int row, int col)
  413.             {
  414.                 SetRange(col, row);
  415.                 m_Range.Borders.LineStyle = 1;
  416.             }
  417.             public void setline(int srow, int scol, int erow, int ecol, int linetype)
  418.             {
  419.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders.LineStyle = linetype;
  420.             }
  421.             public void setlinebold(int srow, int scol, int erow, int ecol, int linetype)
  422.             {
  423.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders.LineStyle = linetype;
  424.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗 
  425.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗 
  426.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗 
  427.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗
  428.             }
  429.             public void setline_left(int srow, int scol, int erow, int ecol, int linetype)
  430.             {
  431.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = linetype;
  432.             }
  433.             public void setline_right(int srow, int scol, int erow, int ecol, int linetype)
  434.             {
  435.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = linetype;
  436.             }
  437.             public void setline_top(int srow, int scol, int erow, int ecol, int linetype)
  438.             {
  439.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = linetype;
  440.             }
  441.             public void setline_btoon(int srow, int scol, int erow, int ecol, int linetype)
  442.             {
  443.                 m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]).Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = linetype;
  444.             }
  445.             //赋值2
  446.             public void SetCellValue2(int srow, int scol, int erow, int ecol, string value)
  447.             {
  448.                 m_Range = m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]);
  449.                 m_Range.Value2 = value;
  450.                 m_Range.Font.Name = "Arial";
  451.                 m_Range.Font.Size = 9;
  452.             }
  453.             public void SetCellbolk2(int srow, int scol, int erow, int ecol)
  454.             {
  455.                 m_Range = m_objSheet.get_Range(m_objExcel.Cells[srow, scol], m_objExcel.Cells[erow, ecol]);
  456.                 m_Range.Font.Bold = true;
  457.             }
  458.             public void save()
  459.             {
  460.                 m_objBook.SaveAs(@"E:/Demo.xls", miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
  461.                 m_objBook.Close(miss, miss, miss);
  462.                 m_objExcel.Quit();
  463.                 GC.Collect();
  464.             }
  465.         }
  466.     }
  467. }