[C#] ExcelTools工具类2 →→→→→进入此内容的聊天室

来自 , 2020-04-29, 写在 C#, 查看 107 次.
URL http://www.code666.cn/view/2417dc8a
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using Microsoft.Office.Interop;
  12. using Microsoft.Office.Core;
  13.  
  14.  
  15. namespace ExcelEdit
  16. {
  17. /// <SUMMARY>
  18. /// ExcelEdit 的摘要说明
  19. /// </SUMMARY>
  20.     public class ExcelEdit
  21.     {
  22.         public string mFilename;
  23.         public Excel.Application app;
  24.         public Excel.Workbooks wbs;
  25.         public Excel.Workbook wb;
  26.         public Excel.Worksheets wss;
  27.         public Excel.Worksheet ws;
  28.         public ExcelEdit()
  29.         {
  30.             //
  31.             // TODO: 在此处添加构造函数逻辑
  32.             //
  33.         }
  34.         public void Create()//创建一个Excel对象
  35.         {
  36.             app = new Excel.Application();
  37.             wbs = app.Workbooks;
  38.             wb = wbs.Add(true);
  39.         }
  40.         public void Open(string FileName)//打开一个Excel文件
  41.         {
  42.             app = new Excel.Application();
  43.             wbs = app.Workbooks;
  44.             wb = wbs.Add(FileName);
  45.             //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
  46.             //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  47.             mFilename = FileName;
  48.         }
  49.         public Excel.Worksheet GetSheet(string SheetName)
  50.        //获取一个工作表
  51.         {
  52.             Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[SheetName];
  53.             return s;
  54.         }
  55.         public Excel.Worksheet AddSheet(string SheetName)
  56.         //添加一个工作表
  57.         {
  58.             Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  59.             s.Name = SheetName;
  60.             return s;
  61.         }
  62.  
  63.         public void DelSheet(string SheetName)//删除一个工作表
  64.         {
  65.             ((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
  66.         }
  67.         public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一
  68.         {
  69.             Excel.Worksheet s = (Excel.Worksheet)wb.Worksheets[OldSheetName];
  70.             s.Name = NewSheetName;
  71.             return s;
  72.         }
  73.  
  74.         public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二
  75.         {
  76.  
  77.             Sheet.Name = NewSheetName;
  78.  
  79.             return Sheet;
  80.         }
  81.  
  82.         public void SetCellValue(Excel.Worksheet ws, int x, int y, object value)
  83. //ws:要设值的工作表     X行Y列     value   值
  84.         {
  85.             ws.Cells[x, y] = value;
  86.         }
  87.         public void SetCellValue(string ws, int x, int y, object value)
  88. //ws:要设值的工作表的名称 X行Y列 value 值
  89.         {
  90.  
  91.             GetSheet(ws).Cells[x, y] = value;
  92.         }
  93.  
  94.         public void SetCellProperty(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
  95. //设置一个单元格的属性   字体,   大小,颜色   ,对齐方式
  96.         {
  97.             name = "宋体";
  98.             size = 12;
  99.             color = Excel.Constants.xlAutomatic;
  100.             HorizontalAlignment = Excel.Constants.xlRight;
  101.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  102.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  103.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
  104.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  105.         }
  106.  
  107.         public void SetCellProperty(string wsn, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)
  108.         {
  109.             //name = "宋体";
  110.             //size = 12;
  111.             //color = Excel.Constants.xlAutomatic;
  112.             //HorizontalAlignment = Excel.Constants.xlRight;
  113.  
  114.             Excel.Worksheet ws = GetSheet(wsn);
  115.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  116.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  117.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
  118.  
  119.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  120.         }
  121.  
  122.  
  123.         public void UniteCells(Excel.Worksheet ws, int x1, int y1, int x2, int y2)
  124. //合并单元格
  125.         {
  126.             ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
  127.         }
  128.  
  129.         public void UniteCells(string ws, int x1, int y1, int x2, int y2)
  130. //合并单元格
  131.         {
  132.             GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);
  133.  
  134.         }
  135.  
  136.  
  137.         public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
  138. //将内存中数据表格插入到Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
  139.         {
  140.  
  141.             for (int i = 0; i <= dt.Rows.Count - 1; i++)
  142.             {
  143.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)
  144.                 {
  145.                     GetSheet(ws).Cells[startX+i, j + startY] = dt.Rows[i][j].ToString();
  146.  
  147.                 }
  148.  
  149.             }
  150.  
  151.         }
  152.         public void InsertTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
  153. //将内存中数据表格插入到Excel指定工作表的指定位置二
  154.         {
  155.  
  156.             for (int i = 0; i <= dt.Rows.Count - 1; i++)
  157.             {
  158.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)
  159.                 {
  160.  
  161.                     ws.Cells[startX+i, j + startY] = dt.Rows[i][j];
  162.  
  163.                 }
  164.  
  165.             }
  166.  
  167.         }
  168.  
  169.  
  170.         public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
  171. //将内存中数据表格添加到Excel指定工作表的指定位置一
  172.         {
  173.  
  174.             for (int i = 0; i <= dt.Rows.Count - 1; i++)
  175.             {
  176.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)
  177.                 {
  178.  
  179.                     GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];
  180.  
  181.                 }
  182.  
  183.             }
  184.  
  185.         }
  186.         public void AddTable(System.Data.DataTable dt, Excel.Worksheet ws, int startX, int startY)
  187. //将内存中数据表格添加到Excel指定工作表的指定位置二
  188.         {
  189.  
  190.  
  191.             for (int i = 0; i <= dt.Rows.Count - 1; i++)
  192.             {
  193.                 for (int j = 0; j <= dt.Columns.Count - 1; j++)
  194.                 {
  195.  
  196.                     ws.Cells[i + startX, j + startY] = dt.Rows[i][j];
  197.  
  198.                 }
  199.             }
  200.  
  201.         }
  202. public void InsertPictures(string Filename, string ws)
  203. //插入图片操作一
  204.         {
  205.             GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse,MsoTriState.msoTrue, 10, 10, 150, 150);
  206. //后面的数字表示位置
  207.         }
  208.  
  209.         //public void InsertPictures(string Filename, string ws, int Height, int Width)
  210. //插入图片操作二
  211.         //{
  212.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  213.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  214.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  215.         //}
  216.         //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
  217. //插入图片操作三
  218.         //{
  219.  
  220.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  221.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
  222.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
  223.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  224.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  225.         //}
  226.  
  227.         public void InsertActiveChart(Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Excel.XlRowCol ChartDataType)
  228. //插入图表操作
  229.         {
  230.             ChartDataType = Excel.XlRowCol.xlColumns;
  231.             wb.Charts.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  232.             {
  233.                 wb.ActiveChart.ChartType = ChartType;
  234.                 wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
  235.                 wb.ActiveChart.Location(Excel.XlChartLocation.xlLocationAsObject, ws);
  236.             }
  237.         }
  238.         public bool Save()
  239. //保存文档
  240.         {
  241.             if (mFilename == "")
  242.             {
  243.                 return false;
  244.             }
  245.             else
  246.             {
  247.                 try
  248.                 {
  249.                     wb.Save();
  250.                     return true;
  251.                 }
  252.  
  253.                 catch (Exception ex)
  254.                 {
  255.                     return false;
  256.                 }
  257.             }
  258.         }
  259.         public bool SaveAs(object FileName)
  260. //文档另存为
  261.         {
  262.             try
  263.             {
  264.                 wb.SaveAs(FileName,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  265.                 return true;
  266.              
  267.             }
  268.  
  269.             catch (Exception ex)
  270.             {
  271.                 return false;
  272.                
  273.             }
  274.         }
  275.         public void Close()
  276. //关闭一个Excel对象,销毁对象
  277.         {
  278.             //wb.Save();
  279.             wb.Close(Type.Missing,Type.Missing,Type.Missing);
  280.             wbs.Close();
  281.             app.Quit();
  282.             wb = null;
  283.             wbs = null;
  284.             app = null;
  285.             GC.Collect();
  286.         }
  287.     }
  288. }
  289.  

回复 "ExcelTools工具类2"

这儿你可以回复上面这条便签

captcha