操作EXCEL代码(c#完全版)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值