转载一个EXCEL操作类

Excel操作
  1. /**  
  2.  * Excel控制类  
  3.  * 作者:王文斌  
  4.  * 版本:bate2  2008-6-6  
  5.  * 备注:部分方法未经测试,使用时请注意。新增复制模板方法。支持26列以上表格。  
  6.  * 使用方法:确保已安装office03,并安装Excel的.net开发支持  
  7.  * 引用com口dll  
  8.  * */  
  9.   
  10. using System;   
  11.   
  12. using System.Reflection;   
  13.   
  14. using Microsoft.Office.Interop.Excel;   
  15.   
  16. using Microsoft.Office.Core;   
  17.   
  18. using System.Drawing;   
  19.   
  20.   
  21.   
  22. namespace WWBClassLib.Excel   
  23.   
  24. {   
  25.   
  26.     /// <summary>   
  27.   
  28.     /// ExcelClass 的摘要说明。   
  29.   
  30.     /// </summary>   
  31.   
  32.     public class ExcelCtrl   
  33.   
  34.     {   
  35.   
  36.         /// <summary>   
  37.   
  38.         /// 构建ExcelClass类   
  39.   
  40.         /// </summary>   
  41.   
  42.         public ExcelCtrl()   
  43.   
  44.         {   
  45.   
  46.             this.m_objExcel = new Microsoft.Office.Interop.Excel.Application();   
  47.   
  48.         }   
  49.   
  50.         /// <summary>   
  51.   
  52.         /// 构建ExcelClass类   
  53.   
  54.         /// </summary>   
  55.   
  56.         /// <param name="objExcel">Excel.Application</param>   
  57.   
  58.         public ExcelCtrl(Microsoft.Office.Interop.Excel.Application objExcel)   
  59.   
  60.         {   
  61.   
  62.             this.m_objExcel = objExcel;   
  63.   
  64.         }   
  65.   
  66.   
  67.   
  68.         /// <summary>   
  69.   
  70.         /// 列标号   
  71.   
  72.         /// </summary>   
  73.   
  74.         private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";   
  75.   
  76.   
  77.   
  78.         /// <summary>   
  79.   
  80.         /// 获取描述区域的字符   
  81.   
  82.         /// </summary>   
  83.   
  84.         /// <param name="x"></param>   
  85.   
  86.         /// <param name="y"></param>   
  87.   
  88.         /// <returns></returns>   
  89.   
  90.         public string GetAix(int x, int y)   
  91.   
  92.         {   
  93.   
  94.             char[] AChars = AList.ToCharArray();   
  95.   
  96.             int i = x % 26;   
  97.   
  98.             int j = x / 26;   
  99.   
  100.   
  101.   
  102.             //if (x >= 26) { return ""; }   
  103.   
  104.             string s = "";   
  105.   
  106.             if (j > 0)   
  107.   
  108.             {   
  109.   
  110.   
  111.   
  112.                 s = s + AChars[j - 1].ToString() + AChars[i].ToString();   
  113.   
  114.   
  115.   
  116.   
  117.   
  118.             }   
  119.   
  120.             else  
  121.   
  122.             {   
  123.   
  124.                 s = s + AChars[x - 1].ToString();   
  125.   
  126.             }   
  127.   
  128.             //s = s + AChars[x - 1].ToString();   
  129.   
  130.             s = s + y.ToString();   
  131.   
  132.             return s;   
  133.   
  134.         }   
  135.   
  136.   
  137.   
  138.         /// <summary>   
  139.   
  140.         /// 获取当前活动Sheet的Range的值   
  141.   
  142.         /// </summary>   
  143.   
  144.         /// <param name="y"></param>   
  145.   
  146.         /// <param name="x"></param>   
  147.   
  148.         /// <returns></returns>   
  149.   
  150.         public string GetRangeText(int y, int x)   
  151.   
  152.         {   
  153.   
  154.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);   
  155.   
  156.             return range.Text.ToString();   
  157.   
  158.         }   
  159.   
  160.   
  161.   
  162.         /// <summary>   
  163.   
  164.         /// 给单元格赋值1   
  165.   
  166.         /// </summary>   
  167.   
  168.         /// <param name="x">行号</param>   
  169.   
  170.         /// <param name="y">列号</param>   
  171.   
  172.         /// <param name="align">对齐(CENTER、LEFT、RIGHT)</param>   
  173.   
  174.         /// <param name="text">值</param>   
  175.   
  176.         public void setValue(int y, int x, string align, string text)   
  177.   
  178.         {   
  179.   
  180.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);   
  181.   
  182.             range.set_Value(miss, text);   
  183.   
  184.             if (align.ToUpper() == "CENTER")   
  185.   
  186.             {   
  187.   
  188.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;   
  189.   
  190.             }   
  191.   
  192.             if (align.ToUpper() == "LEFT")   
  193.   
  194.             {   
  195.   
  196.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;   
  197.   
  198.             }   
  199.   
  200.             if (align.ToUpper() == "RIGHT")   
  201.   
  202.             {   
  203.   
  204.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;   
  205.   
  206.             }   
  207.   
  208.         }   
  209.   
  210.   
  211.   
  212.         /// <summary>   
  213.   
  214.         /// 给单元格赋值2   
  215.   
  216.         /// </summary>   
  217.   
  218.         /// <param name="x">行号</param>   
  219.   
  220.         /// <param name="y">列号</param>   
  221.   
  222.         /// <param name="text">值</param>   
  223.   
  224.         public void setValue(int y, int x, string text)   
  225.   
  226.         {   
  227.   
  228.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);   
  229.   
  230.             range.set_Value(miss, text);   
  231.   
  232.         }   
  233.   
  234.   
  235.   
  236.         /// <summary>   
  237.   
  238.         /// 给单元格赋值3   
  239.   
  240.         /// </summary>   
  241.   
  242.         /// <param name="x">行号</param>   
  243.   
  244.         /// <param name="y">列号</param>   
  245.   
  246.         /// <param name="text">值</param>   
  247.   
  248.         /// <param name="font">字符格式</param>   
  249.   
  250.         /// <param name="color">颜色</param>   
  251.   
  252.         public void setValue(int y, int x, string text, System.Drawing.Font font, System.Drawing.Color color)   
  253.   
  254.         {   
  255.   
  256.             this.setValue(x, y, text);   
  257.   
  258.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);   
  259.   
  260.             range.Font.Size = font.Size;   
  261.   
  262.             range.Font.Bold = font.Bold;   
  263.   
  264.             range.Font.Color = color;   
  265.   
  266.             range.Font.Name = font.Name;   
  267.   
  268.             range.Font.Italic = font.Italic;   
  269.   
  270.             range.Font.Underline = font.Underline;   
  271.   
  272.         }   
  273.   
  274.   
  275.   
  276.         /// <summary>   
  277.   
  278.         /// 给单元格赋值4   
  279.   
  280.         /// </summary>   
  281.   
  282.         /// <param name="x">行号</param>   
  283.   
  284.         /// <param name="y">列号</param>   
  285.   
  286.         /// <param name="text">值</param>   
  287.   
  288.         /// <param name="font">字符格式</param>   
  289.   
  290.         public void setValue(int y, int x, string text, float size)   
  291.   
  292.         {   
  293.   
  294.             this.setValue(x, y, text);   
  295.   
  296.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);   
  297.   
  298.             range.Font.Size = size;   
  299.   
  300.   
  301.   
  302.         }   
  303.   
  304.   
  305.   
  306.         //public void CopeTo(string fromSheetName,string startCell,string endCell,string ToSheetName)   
  307.   
  308.         //{   
  309.   
  310.   
  311.   
  312.         //    Microsoft.Office.Interop.Excel.Worksheet fromsheet = GetSheet(fromSheetName);   
  313.   
  314.         //    Microsoft.Office.Interop.Excel.Worksheet tosheet = GetSheet(ToSheetName);   
  315.   
  316.         //    Microsoft.Office.Interop.Excel.Range rangefrom = fromsheet.get_Range(startCell, endCell);   
  317.   
  318.         //    Microsoft.Office.Interop.Excel.Range rangeto = tosheet.get_Range(startCell, endCell);   
  319.   
  320.         //    //rangeto.Copy(rangefrom);   
  321.   
  322.         //    tosheet.Paste(rangefrom, rangeto);   
  323.   
  324.         //}   
  325.   
  326.   
  327.   
  328.         /// <summary>   
  329.   
  330.         /// 复制sheet   
  331.   
  332.         /// 修正完成,重命名即可   
  333.   
  334.         ///    
  335.   
  336.         /// </summary>   
  337.   
  338.         /// <param name="fromSheetName"></param>   
  339.   
  340.         /// <param name="ToSheetName"></param>   
  341.   
  342.         /// <returns></returns>   
  343.   
  344.         public void CopySheet(string fromSheetName, string ToSheetName)   
  345.   
  346.         {   
  347.   
  348.             Microsoft.Office.Interop.Excel.Worksheet fromsheet = GetSheet(fromSheetName);   
  349.   
  350.   
  351.   
  352.             fromsheet.Copy(fromsheet, miss);   
  353.   
  354.   
  355.   
  356.             ReNameSheet(fromSheetName + " (2)", ToSheetName);   
  357.   
  358.   
  359.   
  360.         }   
  361.   
  362.         /// <summary>   
  363.   
  364.         /// 插入新行 将设定行拷贝,然后插入空行,再将设定行粘贴到新行上   
  365.   
  366.         /// </summary>   
  367.   
  368.         /// <param name="y">模板行号</param>   
  369.   
  370.         public void insertRow(int y)   
  371.   
  372.         {   
  373.   
  374.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(GetAix(1, y), GetAix(25, y));   
  375.   
  376.             range.Copy(miss);   
  377.   
  378.             range.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, miss);   
  379.   
  380.             range.get_Range(GetAix(1, y), GetAix(25, y));   
  381.   
  382.             range.Select();   
  383.   
  384.             sheet.Paste(miss, miss);   
  385.   
  386.   
  387.   
  388.         }   
  389.   
  390.   
  391.   
  392.         //public void insertCopyRow(int x1,int y1,int x2,int y2)   
  393.   
  394.         //{   
  395.   
  396.         //    Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(GetAix(1, y), GetAix(25, y));   
  397.   
  398.         //    range.Copy(miss);   
  399.   
  400.         //    range.Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown, miss);   
  401.   
  402.         //    range.get_Range(GetAix(1, y), GetAix(25, y));   
  403.   
  404.         //    range.Select();   
  405.   
  406.         //    sheet.Paste(miss, miss);   
  407.   
  408.   
  409.   
  410.         //}   
  411.   
  412.   
  413.   
  414.         ///// <summary>   
  415.   
  416.         ///// 把剪切内容粘贴到指定区域   
  417.   
  418.         ///// </summary>   
  419.   
  420.         public void past(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)   
  421.   
  422.         {   
  423.   
  424.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(GetAix(x1, y1), GetAix(x2, y2));   
  425.   
  426.             range.Copy(miss);   
  427.   
  428.             sheet.Paste(sheet.get_Range(this.GetAix(x3, y3), this.GetAix(x4, y4)), range);   
  429.   
  430.         }   
  431.   
  432.   
  433.   
  434.         /// <summary>   
  435.   
  436.         /// 设置边框   
  437.   
  438.         /// </summary>   
  439.   
  440.         /// <param name="x1"></param>   
  441.   
  442.         /// <param name="y1"></param>   
  443.   
  444.         /// <param name="x2"></param>   
  445.   
  446.         /// <param name="y2"></param>   
  447.   
  448.         /// <param name="Width"></param>   
  449.   
  450.         public void setBorder(int x1, int y1, int x2, int y2, int Width)   
  451.   
  452.         {   
  453.   
  454.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));   
  455.   
  456.             range.Borders.Weight = Width;   
  457.   
  458.         }   
  459.   
  460.   
  461.   
  462.   
  463.   
  464.   
  465.   
  466.         /// <summary>   
  467.   
  468.         /// 合并单元格,并设置居中方式 测试有问题 修正   
  469.   
  470.         /// </summary>   
  471.   
  472.         /// <param name="x1"></param>   
  473.   
  474.         /// <param name="y1"></param>   
  475.   
  476.         /// <param name="x2"></param>   
  477.   
  478.         /// <param name="y2"></param>   
  479.   
  480.         /// <param name="align">居中方式:center,left,right</param>   
  481.   
  482.         public void mergeCell(int x1, int y1, int x2, int y2, string align)   
  483.   
  484.         {   
  485.   
  486.             UniteCells(sheet, x1, y1, x2, y2);   
  487.   
  488.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));   
  489.   
  490.             //range.Merge(true);   
  491.   
  492.             if (align.ToUpper() == "CENTER")   
  493.   
  494.             {   
  495.   
  496.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;   
  497.   
  498.             }   
  499.   
  500.             if (align.ToUpper() == "LEFT")   
  501.   
  502.             {   
  503.   
  504.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;   
  505.   
  506.             }   
  507.   
  508.             if (align.ToUpper() == "RIGHT")   
  509.   
  510.             {   
  511.   
  512.                 range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;   
  513.   
  514.             }   
  515.   
  516.         }   
  517.   
  518.         /// <summary>   
  519.   
  520.         /// 获取Range区域   
  521.   
  522.         /// </summary>   
  523.   
  524.         /// <param name="x1"></param>   
  525.   
  526.         /// <param name="y1"></param>   
  527.   
  528.         /// <param name="x2"></param>   
  529.   
  530.         /// <param name="y2"></param>   
  531.   
  532.         /// <returns></returns>   
  533.   
  534.         public Microsoft.Office.Interop.Excel.Range getRange(int x1, int y1, int x2, int y2)   
  535.   
  536.         {   
  537.   
  538.             Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(this.GetAix(x1, y1), this.GetAix(x2, y2));   
  539.   
  540.             return range;   
  541.   
  542.         }   
  543.   
  544.   
  545.   
  546.         private object miss = Missing.Value; //忽略的参数OLENULL    
  547.   
  548.         private Microsoft.Office.Interop.Excel.Application m_objExcel;//Excel应用程序实例    
  549.   
  550.         private Microsoft.Office.Interop.Excel.Workbooks m_objBooks;//工作表集合    
  551.   
  552.         private Microsoft.Office.Interop.Excel.Workbook m_objBook;//当前操作的工作表    
  553.   
  554.         private Microsoft.Office.Interop.Excel.Worksheet sheet;//当前操作的表格    
  555.   
  556.   
  557.   
  558.         /// <summary>   
  559.   
  560.         /// 活动工作Sheet   
  561.   
  562.         /// </summary>   
  563.   
  564.         public Microsoft.Office.Interop.Excel.Worksheet CurrentSheet   
  565.   
  566.         {   
  567.   
  568.             get  
  569.   
  570.             {   
  571.   
  572.                 return sheet;   
  573.   
  574.             }   
  575.   
  576.             set  
  577.   
  578.             {   
  579.   
  580.                 this.sheet = value;   
  581.   
  582.             }   
  583.   
  584.         }   
  585.   
  586.   
  587.   
  588.         /// <summary>   
  589.   
  590.         /// 获取活动WorkBooks   
  591.   
  592.         /// </summary>   
  593.   
  594.         public Microsoft.Office.Interop.Excel.Workbooks CurrentWorkBooks   
  595.   
  596.         {   
  597.   
  598.             get  
  599.   
  600.             {   
  601.   
  602.                 return this.m_objBooks;   
  603.   
  604.             }   
  605.   
  606.             set  
  607.   
  608.             {   
  609.   
  610.                 this.m_objBooks = value;   
  611.   
  612.             }   
  613.   
  614.         }   
  615.   
  616.   
  617.   
  618.         /// <summary>   
  619.   
  620.         /// 获取活动WorkBook   
  621.   
  622.         /// </summary>   
  623.   
  624.         public Microsoft.Office.Interop.Excel.Workbook CurrentWorkBook   
  625.   
  626.         {   
  627.   
  628.             get  
  629.   
  630.             {   
  631.   
  632.                 return this.m_objBook;   
  633.   
  634.             }   
  635.   
  636.             set  
  637.   
  638.             {   
  639.   
  640.                 this.m_objBook = value;   
  641.   
  642.             }   
  643.   
  644.         }   
  645.   
  646.   
  647.   
  648.         /// <summary>   
  649.   
  650.         /// 打开Excel文件   
  651.   
  652.         /// </summary>   
  653.   
  654.         /// <param name="filename">路径</param>   
  655.   
  656.         public void OpenExcelFile(string filename)   
  657.   
  658.         {   
  659.   
  660.             UserControl(false);   
  661.   
  662.   
  663.   
  664.             m_objExcel.Workbooks.Open(filename, miss, miss, miss, miss, miss, miss, miss,   
  665.   
  666.                                     miss, miss, miss, miss, miss, miss, miss);   
  667.   
  668.   
  669.   
  670.             m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks;   
  671.   
  672.   
  673.   
  674.             m_objBook = m_objExcel.ActiveWorkbook;   
  675.   
  676.             sheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet;   
  677.   
  678.         }   
  679.   
  680.   
  681.   
  682.         public void UserControl(bool usercontrol)   
  683.   
  684.         {   
  685.   
  686.             if (m_objExcel == null) { return; }   
  687.   
  688.             m_objExcel.UserControl = usercontrol;   
  689.   
  690.             m_objExcel.DisplayAlerts = usercontrol;   
  691.   
  692.             m_objExcel.Visible = usercontrol;   
  693.   
  694.         }   
  695.   
  696.   
  697.   
  698.         /// <summary>   
  699.   
  700.         /// 创建Excel文件   
  701.   
  702.         /// </summary>   
  703.   
  704.         public void CreateExceFile()   
  705.   
  706.         {   
  707.   
  708.             UserControl(false);   
  709.   
  710.             m_objBooks = (Microsoft.Office.Interop.Excel.Workbooks)m_objExcel.Workbooks;   
  711.   
  712.             m_objBook = (Microsoft.Office.Interop.Excel.Workbook)(m_objBooks.Add(miss));   
  713.   
  714.             sheet = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.ActiveSheet;   
  715.   
  716.         }   
  717.   
  718.   
  719.   
  720.         /// <summary>   
  721.   
  722.         /// Excel文件另存为   
  723.   
  724.         /// </summary>   
  725.   
  726.         /// <param name="FileName"></param>   
  727.   
  728.         public void SaveAs(string FileName)   
  729.   
  730.         {   
  731.   
  732.   
  733.   
  734.             m_objBook.SaveAs(FileName, miss, miss, miss, miss,   
  735.   
  736.              miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,   
  737.   
  738.              Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges,   
  739.   
  740.              miss, miss, miss, miss);   
  741.   
  742.             //m_objBook.Close(false, miss, miss);    
  743.   
  744.         }   
  745.   
  746.   
  747.   
  748.         /// <summary>   
  749.   
  750.         /// 释放Excel文件   
  751.   
  752.         /// </summary>   
  753.   
  754.         public void ReleaseExcel()   
  755.   
  756.         {   
  757.   
  758.             m_objExcel.Quit();   
  759.   
  760.             System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objExcel);   
  761.   
  762.             System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBooks);   
  763.   
  764.             System.Runtime.InteropServices.Marshal.ReleaseComObject((object)m_objBook);   
  765.   
  766.             System.Runtime.InteropServices.Marshal.ReleaseComObject((object)sheet);   
  767.   
  768.             m_objExcel = null;   
  769.   
  770.             m_objBooks = null;   
  771.   
  772.             m_objBook = null;   
  773.   
  774.             sheet = null;   
  775.   
  776.             GC.Collect();   
  777.   
  778.         }   
  779.   
  780.   
  781.   
  782.         /////////////////////////////////   
  783.   
  784.         public bool KillAllExcelApp()   
  785.   
  786.         {   
  787.   
  788.             try  
  789.   
  790.             {   
  791.   
  792.                 if (m_objExcel != null// isRunning是判断xlApp是怎么启动的flag.   
  793.   
  794.                 {   
  795.   
  796.                     m_objExcel.Quit();   
  797.   
  798.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);   
  799.   
  800.                     //释放COM组件,其实就是将其引用计数减1   
  801.   
  802.                     //System.Diagnostics.Process theProc;   
  803.   
  804.                     foreach (System.Diagnostics.Process theProc in System.Diagnostics.Process.GetProcessesByName("EXCEL"))   
  805.   
  806.                     {   
  807.   
  808.                         //先关闭图形窗口。如果关闭失败...有的时候在状态里看不到图形窗口的excel了,   
  809.   
  810.                         //但是在进程里仍然有EXCEL.EXE的进程存在,那么就需要杀掉它:p   
  811.   
  812.                         if (theProc.CloseMainWindow() == false)   
  813.   
  814.                         {   
  815.   
  816.                             theProc.Kill();   
  817.   
  818.                         }   
  819.   
  820.                     }   
  821.   
  822.                     m_objExcel = null;   
  823.   
  824.                     return true;   
  825.   
  826.                 }   
  827.   
  828.             }   
  829.   
  830.             catch  
  831.   
  832.             {   
  833.   
  834.                 return false;   
  835.   
  836.             }   
  837.   
  838.             return true;   
  839.   
  840.         }   
  841.   
  842.         /////////////////////////////////////////////   
  843.   
  844.   
  845.   
  846.         /// <summary>   
  847.   
  848.         /// 获取一个工作表   
  849.   
  850.         /// </summary>   
  851.   
  852.         /// <param name="SheetName">Sheet名</param>   
  853.   
  854.         /// <returns>Sheet对象</returns>   
  855.   
  856.         public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string SheetName)   
  857.   
  858.         {   
  859.   
  860.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets[SheetName];   
  861.   
  862.             return s;   
  863.   
  864.         }   
  865.   
  866.   
  867.   
  868.         /// <summary>   
  869.   
  870.         /// 添加一个工作表   
  871.   
  872.         /// </summary>   
  873.   
  874.         /// <param name="SheetName"></param>   
  875.   
  876.         /// <returns></returns>   
  877.   
  878.         public Microsoft.Office.Interop.Excel.Worksheet AddSheetWithReturn(string SheetName)   
  879.   
  880.         {   
  881.   
  882.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);   
  883.   
  884.             s.Name = SheetName;   
  885.   
  886.             return s;   
  887.   
  888.         }   
  889.   
  890.   
  891.   
  892.         /// <summary>   
  893.   
  894.         /// 添加一个工作表   
  895.   
  896.         /// </summary>   
  897.   
  898.         public void AddSheet(string SheetName)   
  899.   
  900.         {   
  901.   
  902.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);   
  903.   
  904.             s.Name = SheetName;   
  905.   
  906.   
  907.   
  908.         }   
  909.   
  910.   
  911.   
  912.         /// <summary>   
  913.   
  914.         /// 删除一个工作表   
  915.   
  916.         /// </summary>   
  917.   
  918.         /// <param name="SheetName"></param>   
  919.   
  920.         public void DelSheet(string SheetName)   
  921.   
  922.         {   
  923.   
  924.             ((Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets[SheetName]).Delete();   
  925.   
  926.         }   
  927.   
  928.   
  929.   
  930.         /// <summary>   
  931.   
  932.         /// 重命名工作表   
  933.   
  934.         /// </summary>   
  935.   
  936.         /// <param name="OldSheetName">原名</param>   
  937.   
  938.         /// <param name="NewSheetName">新名</param>   
  939.   
  940.         /// <returns></returns>   
  941.   
  942.         public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一   
  943.   
  944.         {   
  945.   
  946.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)m_objBook.Worksheets[OldSheetName];   
  947.   
  948.             s.Name = NewSheetName;   
  949.   
  950.             return s;   
  951.   
  952.         }   
  953.   
  954.   
  955.   
  956.         public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(Microsoft.Office.Interop.Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二   
  957.   
  958.         {   
  959.   
  960.   
  961.   
  962.             Sheet.Name = NewSheetName;   
  963.   
  964.   
  965.   
  966.             return Sheet;   
  967.   
  968.         }   
  969.   
  970.   
  971.   
  972.   
  973.   
  974.         /// <summary>   
  975.   
  976.         /// 设置单元格值   
  977.   
  978.         /// </summary>   
  979.   
  980.         /// <param name="ws"></param>   
  981.   
  982.         /// <param name="x"></param>   
  983.   
  984.         /// <param name="y"></param>   
  985.   
  986.         /// <param name="value"></param>   
  987.   
  988.         public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet ws, int x, int y, object value)   
  989.   
  990.         {   
  991.   
  992.             ws.Cells[y, x] = value;   
  993.   
  994.         }   
  995.   
  996.   
  997.   
  998.         /// <summary>   
  999.   
  1000.         /// 设置单元格值   
  1001.   
  1002.         /// </summary>   
  1003.   
  1004.         /// <param name="ws">sheet名</param>   
  1005.   
  1006.         /// <param name="x"></param>   
  1007.   
  1008.         /// <param name="y"></param>   
  1009.   
  1010.         /// <param name="value"></param>   
  1011.   
  1012.         public void SetCellValue(string ws, int x, int y, object value)   
  1013.   
  1014.         {   
  1015.   
  1016.   
  1017.   
  1018.             GetSheet(ws).Cells[y, x] = value;   
  1019.   
  1020.         }   
  1021.   
  1022.   
  1023.   
  1024.   
  1025.   
  1026.         public void SetCellProperty(Microsoft.Office.Interop.Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Microsoft.Office.Interop.Excel.Constants color, Microsoft.Office.Interop.Excel.Constants HorizontalAlignment)   
  1027.   
  1028.         //设置一个单元格的属性   字体,   大小,颜色   ,对齐方式   
  1029.   
  1030.         {   
  1031.   
  1032.             //name = "宋体";   
  1033.   
  1034.             //size = 12;   
  1035.   
  1036.             color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;   
  1037.   
  1038.             HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;   
  1039.   
  1040.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Name = name;   
  1041.   
  1042.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Size = size;   
  1043.   
  1044.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Color = color;   
  1045.   
  1046.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).HorizontalAlignment = HorizontalAlignment;   
  1047.   
  1048.         }   
  1049.   
  1050.   
  1051.   
  1052.         /// <summary>   
  1053.   
  1054.         /// 设置单元格属性   
  1055.   
  1056.         /// </summary>   
  1057.   
  1058.         /// <param name="wsn">Sheet页名称</param>   
  1059.   
  1060.         /// <param name="Startx">x1</param>   
  1061.   
  1062.         /// <param name="Starty">y1</param>   
  1063.   
  1064.         /// <param name="Endx">x2</param>   
  1065.   
  1066.         /// <param name="Endy">y2</param>   
  1067.   
  1068.         /// <param name="size">字体大小</param>   
  1069.   
  1070.         /// <param name="name">字体名称</param>   
  1071.   
  1072.         /// <param name="color">字体颜色</param>   
  1073.   
  1074.         /// <param name="HorizontalAlignment">对齐方式</param>   
  1075.   
  1076.         public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Microsoft.Office.Interop.Excel.Constants color, Microsoft.Office.Interop.Excel.Constants HorizontalAlignment)   
  1077.   
  1078.         {   
  1079.   
  1080.             //name = "宋体";   
  1081.   
  1082.             //size = 12;   
  1083.   
  1084.             //color = Excel.Constants.xlAutomatic;   
  1085.   
  1086.             //HorizontalAlignment = Excel.Constants.xlRight;   
  1087.   
  1088.   
  1089.   
  1090.   
  1091.   
  1092.             Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);   
  1093.   
  1094.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Name = name;   
  1095.   
  1096.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Size = size;   
  1097.   
  1098.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Color = color;   
  1099.   
  1100.   
  1101.   
  1102.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).HorizontalAlignment = HorizontalAlignment;   
  1103.   
  1104.         }   
  1105.   
  1106.   
  1107.   
  1108.         /// <summary>   
  1109.   
  1110.         /// 设置单元格属性   
  1111.   
  1112.         /// </summary>   
  1113.   
  1114.         /// <param name="wsn">Sheet页名称</param>   
  1115.   
  1116.         /// <param name="Startx">x1</param>   
  1117.   
  1118.         /// <param name="Starty">y1</param>   
  1119.   
  1120.         /// <param name="Endx">x2</param>   
  1121.   
  1122.         /// <param name="Endy">y2</param>   
  1123.   
  1124.         /// <param name="size">字体大小</param>   
  1125.   
  1126.         /// <param name="name">字体名称</param>   
  1127.   
  1128.         /// <param name="color">字体颜色名称(系统预定义颜色)</param>   
  1129.   
  1130.         /// <param name="HorizontalAlignment">对齐方式</param>   
  1131.   
  1132.         public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, string colorName, string HorizontalAlignment)   
  1133.   
  1134.         {   
  1135.   
  1136.             //name = "宋体";   
  1137.   
  1138.             //size = 12;   
  1139.   
  1140.             //color = Excel.Constants.xlAutomatic;   
  1141.   
  1142.             //HorizontalAlignment = Excel.Constants.xlRight;   
  1143.   
  1144.   
  1145.   
  1146.   
  1147.   
  1148.             Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);   
  1149.   
  1150.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Name = name;   
  1151.   
  1152.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Size = size;   
  1153.   
  1154.   
  1155.   
  1156.             Color color = Color.FromName(colorName);   
  1157.   
  1158.             color = Color.FromArgb(color.B, color.G, color.R);   
  1159.   
  1160.   
  1161.   
  1162.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Font.Color = color.ToArgb();   
  1163.   
  1164.   
  1165.   
  1166.             if (HorizontalAlignment.ToUpper() == "CENTER")   
  1167.   
  1168.             {   
  1169.   
  1170.   
  1171.   
  1172.                 ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; ;   
  1173.   
  1174.             }   
  1175.   
  1176.             if (HorizontalAlignment.ToUpper() == "LEFT")   
  1177.   
  1178.             {   
  1179.   
  1180.                 ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;   
  1181.   
  1182.             }   
  1183.   
  1184.             if (HorizontalAlignment.ToUpper() == "RIGHT")   
  1185.   
  1186.             {   
  1187.   
  1188.                 ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight;   
  1189.   
  1190.             }   
  1191.   
  1192.   
  1193.   
  1194.         }   
  1195.   
  1196.   
  1197.   
  1198.         /// <summary>   
  1199.   
  1200.         /// 设置单元格底色   
  1201.   
  1202.         /// </summary>   
  1203.   
  1204.         /// <param name="wsn">sheet页名称</param>   
  1205.   
  1206.         /// <param name="Startx">x1</param>   
  1207.   
  1208.         /// <param name="Starty">y1</param>   
  1209.   
  1210.         /// <param name="Endx">x2</param>   
  1211.   
  1212.         /// <param name="Endy">y2</param>   
  1213.   
  1214.         /// <param name="colorName">系统颜色名称</param>   
  1215.   
  1216.         public void SetBorderColor(string wsn, int Startx, int Starty, int Endx, int Endy, string colorName)   
  1217.   
  1218.         {   
  1219.   
  1220.             Color color = Color.FromName(colorName);   
  1221.   
  1222.             color = Color.FromArgb(color.B, color.G, color.R);   
  1223.   
  1224.             Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);   
  1225.   
  1226.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Cells.Interior.Color = color.ToArgb();   
  1227.   
  1228.             ws.get_Range(ws.Cells[Starty, Startx], ws.Cells[Endy, Endx]).Cells.Interior.Pattern = Microsoft.Office.Interop.Excel.XlBackground.xlBackgroundAutomatic;   
  1229.   
  1230.   
  1231.   
  1232.         }   
  1233.   
  1234.   
  1235.   
  1236.         //合并单元格 测试ok   
  1237.   
  1238.         public void UniteCells(Microsoft.Office.Interop.Excel.Worksheet ws, int x1, int y1, int x2, int y2)   
  1239.   
  1240.         {   
  1241.   
  1242.             ws.get_Range(ws.Cells[y1, x1], ws.Cells[y2, x2]).Merge(Type.Missing);   
  1243.   
  1244.         }   
  1245.   
  1246.   
  1247.   
  1248.         //合并单元格 测试ok   
  1249.   
  1250.         public void UniteCells(string ws, int x1, int y1, int x2, int y2)   
  1251.   
  1252.         {   
  1253.   
  1254.             GetSheet(ws).get_Range(GetSheet(ws).Cells[y1, x1], GetSheet(ws).Cells[y2, x2]).Merge(Type.Missing);   
  1255.   
  1256.   
  1257.   
  1258.         }   
  1259.   
  1260.   
  1261.   
  1262.         //将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一   
  1263.   
  1264.         public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)   
  1265.   
  1266.         {   
  1267.   
  1268.   
  1269.   
  1270.             for (int i = 0; i < dt.Rows.Count; i++)   
  1271.   
  1272.             {   
  1273.   
  1274.                 for (int j = 0; j < dt.Columns.Count; j++)   
  1275.   
  1276.                 {   
  1277.   
  1278.                     GetSheet(ws).Cells[startY + i, j + startX] = dt.Rows[i][j].ToString();   
  1279.   
  1280.   
  1281.   
  1282.                 }   
  1283.   
  1284.   
  1285.   
  1286.             }   
  1287.   
  1288.   
  1289.   
  1290.         }   
  1291.   
  1292.   
  1293.   
  1294.         //将内存中数据表格插入到Excel指定工作表的指定位置二   
  1295.   
  1296.         public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet ws, int startX, int startY)   
  1297.   
  1298.         {   
  1299.   
  1300.             for (int i = 0; i < dt.Rows.Count; i++)   
  1301.   
  1302.             {   
  1303.   
  1304.                 for (int j = 0; j < dt.Columns.Count; j++)   
  1305.   
  1306.                 {   
  1307.   
  1308.   
  1309.   
  1310.                     ws.Cells[startY + i, j + startX] = dt.Rows[i][j];   
  1311.   
  1312.   
  1313.   
  1314.                 }   
  1315.   
  1316.   
  1317.   
  1318.             }   
  1319.   
  1320.   
  1321.   
  1322.         }   
  1323.   
  1324.   
  1325.   
  1326.         //插入图片操作一   
  1327.   
  1328.         public void InsertPictures(string Filename, string ws, int x, int y, int wight, int lenth)   
  1329.   
  1330.         {   
  1331.   
  1332.             //后面的数字表示位置   
  1333.   
  1334.             GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, x, y, wight, lenth);   
  1335.   
  1336.   
  1337.   
  1338.         }   
  1339.   
  1340.   
  1341.   
  1342.         //public void InsertPictures(string Filename, string ws, int Height, int Width)   
  1343.   
  1344.         //插入图片操作二   
  1345.   
  1346.         //{   
  1347.   
  1348.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);   
  1349.   
  1350.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;   
  1351.   
  1352.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;   
  1353.   
  1354.         //}   
  1355.   
  1356.         //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)   
  1357.   
  1358.         //插入图片操作三   
  1359.   
  1360.         //{   
  1361.   
  1362.   
  1363.   
  1364.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);   
  1365.   
  1366.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);   
  1367.   
  1368.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);   
  1369.   
  1370.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;   
  1371.   
  1372.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;   
  1373.   
  1374.         //}   
  1375.   
  1376.   
  1377.   
  1378.     }   
  1379.   
  1380. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值