学习资料_文档下载_软件应用_程序模板

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

搜索
查看: 4881|回复: 0

C# 处理excel 大全

[复制链接]

295

主题

297

帖子

787

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
787
发表于 2020-9-23 21:22:06 | 显示全部楼层 |阅读模式
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using Microsoft.Office.Interop;
  6. using Microsoft.Office.Core;


  7. namespace Microsoft.Office.Interop.ExcelEdit
  8. {
  9.     /// <SUMMARY>
  10.     /// Microsoft.Office.Interop.ExcelEdit 的摘要说明
  11.     /// </SUMMARY>
  12.     public class ExcelEdit
  13.     {
  14.         public string mFilename;
  15.         public Microsoft.Office.Interop.Excel.Application app;
  16.         public Microsoft.Office.Interop.Excel.Workbooks wbs;
  17.         public Microsoft.Office.Interop.Excel.Workbook wb;
  18.         public Microsoft.Office.Interop.Excel.Worksheets wss;
  19.         public Microsoft.Office.Interop.Excel.Worksheet ws;
  20.         public ExcelEdit()
  21.         {
  22.             //
  23.             // TODO: 在此处添加构造函数逻辑
  24.             //
  25.         }
  26.         public void Create()//创建一个Microsoft.Office.Interop.Excel对象
  27.         {
  28.             app = new Microsoft.Office.Interop.Excel.Application();
  29.             wbs = app.Workbooks;
  30.             wb = wbs.Add(true);
  31.         }
  32.         public void Open(string FileName)//打开一个Microsoft.Office.Interop.Excel文件
  33.         {
  34.             app = new Microsoft.Office.Interop.Excel.Application();
  35.             wbs = app.Workbooks;
  36.             wb = wbs.Add(FileName);
  37.             //wb = wbs.Open(FileName, 0, true, 5,"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);
  38.             //wb = wbs.Open(FileName,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
  39.             mFilename = FileName;
  40.         }
  41.         public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string SheetName)
  42.         //获取一个工作表
  43.         {
  44.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName];
  45.             return s;
  46.         }
  47.         public Microsoft.Office.Interop.Excel.Worksheet AddSheet(string SheetName)
  48.         //添加一个工作表
  49.         {
  50.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  51.             s.Name = SheetName;
  52.             return s;
  53.         }

  54.         public void DelSheet(string SheetName)//删除一个工作表
  55.         {
  56.             ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
  57.         }
  58.         public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一
  59.         {
  60.             Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[OldSheetName];
  61.             s.Name = NewSheetName;
  62.             return s;
  63.         }

  64.         public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(Microsoft.Office.Interop.Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二
  65.         {

  66.             Sheet.Name = NewSheetName;

  67.             return Sheet;
  68.         }

  69.         public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet ws, int x, int y, object value)
  70.         //ws:要设值的工作表     X行Y列     value   值
  71.         {
  72.             ws.Cells[x, y] = value;
  73.         }
  74.         public void SetCellValue(string ws, int x, int y, object value)
  75.         //ws:要设值的工作表的名称 X行Y列 value 值
  76.         {

  77.             GetSheet(ws).Cells[x, y] = value;
  78.         }

  79.         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)
  80.         //设置一个单元格的属性   字体,   大小,颜色   ,对齐方式
  81.         {
  82.             name = "宋体";
  83.             size = 12;
  84.             color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;
  85.             HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;
  86.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  87.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  88.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;
  89.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  90.         }

  91.         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)
  92.         {
  93.             //name = "宋体";
  94.             //size = 12;
  95.             //color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;
  96.             //HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;

  97.             Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);
  98.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;
  99.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;
  100.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;

  101.             ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;
  102.         }


  103.         public void UniteCells(Microsoft.Office.Interop.Excel.Worksheet ws, int x1, int y1, int x2, int y2)
  104.         //合并单元格
  105.         {
  106.             ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);
  107.         }

  108.         public void UniteCells(string ws, int x1, int y1, int x2, int y2)
  109.         //合并单元格
  110.         {
  111.             GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);

  112.         }


  113.         public void InsertTable(System.Data.DataTable dt, string ws, int startX, int startY)
  114. //将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置 为在使用模板时控制格式时使用一
  115.         {

  116.             for (int i = 0; i  <= dt.Rows.Count - 1; i++)
  117.             {
  118.                 for (int j = 0; j  <= dt.Columns.Count - 1; j++)
  119.                 {
  120.                     GetSheet(ws).Cells[startX+i, j + startY] = dt.Rows[i][j].ToString();

  121.                 }

  122.             }

  123.         }
  124.         public void InsertTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet ws, int startX, int startY)
  125. //将内存中数据表格插入到Microsoft.Office.Interop.Excel指定工作表的指定位置二
  126.         {

  127.             for (int i = 0; i  <= dt.Rows.Count - 1; i++)
  128.             {
  129.                 for (int j = 0; j  <= dt.Columns.Count - 1; j++)
  130.                 {

  131.                     ws.Cells[startX+i, j + startY] = dt.Rows[i][j];

  132.                 }

  133.             }

  134.         }


  135.         public void AddTable(System.Data.DataTable dt, string ws, int startX, int startY)
  136. //将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置一
  137.         {

  138.             for (int i = 0; i  <= dt.Rows.Count - 1; i++)
  139.             {
  140.                 for (int j = 0; j  <= dt.Columns.Count - 1; j++)
  141.                 {

  142.                     GetSheet(ws).Cells[i + startX, j + startY] = dt.Rows[i][j];

  143.                 }

  144.             }

  145.         }
  146.         public void AddTable(System.Data.DataTable dt, Microsoft.Office.Interop.Excel.Worksheet ws, int startX, int startY)
  147. //将内存中数据表格添加到Microsoft.Office.Interop.Excel指定工作表的指定位置二
  148.         {


  149.             for (int i = 0; i  <= dt.Rows.Count - 1; i++)
  150.             {
  151.                 for (int j = 0; j  <= dt.Columns.Count - 1; j++)
  152.                 {

  153.                     ws.Cells[i + startX, j + startY] = dt.Rows[i][j];

  154.                 }
  155.             }

  156.         }
  157.         public void InsertPictures(string Filename, string ws)
  158.         //插入图片操作一
  159.         {
  160.             GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  161.             //后面的数字表示位置
  162.         }

  163.         //public void InsertPictures(string Filename, string ws, int Height, int Width)
  164.         //插入图片操作二
  165.         //{
  166.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  167.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  168.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  169.         //}
  170.         //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)
  171.         //插入图片操作三
  172.         //{

  173.         //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);
  174.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);
  175.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);
  176.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;
  177.         //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;
  178.         //}

  179.         public void InsertActiveChart(Microsoft.Office.Interop.Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Microsoft.Office.Interop.Excel.XlRowCol ChartDataType)
  180.         //插入图表操作
  181.         {
  182.             ChartDataType = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns;
  183.             wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  184.             {
  185.                 wb.ActiveChart.ChartType = ChartType;
  186.                 wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);
  187.                 wb.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, ws);
  188.             }
  189.         }
  190.         public bool Save()
  191.         //保存文档
  192.         {
  193.             if (mFilename == "")
  194.             {
  195.                 return false;
  196.             }
  197.             else
  198.             {
  199.                 try
  200.                 {
  201.                     wb.Save();
  202.                     return true;
  203.                 }

  204.                 catch (Exception ex)
  205.                 {
  206.                     return false;
  207.                 }
  208.             }
  209.         }
  210.         public bool SaveAs(object FileName)
  211.         //文档另存为
  212.         {
  213.             try
  214.             {
  215.                 wb.SaveAs(FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
  216.                 return true;

  217.             }

  218.             catch (Exception ex)
  219.             {
  220.                 return false;

  221.             }
  222.         }
  223.         public void Close()
  224.         //关闭一个Microsoft.Office.Interop.Excel对象,销毁对象
  225.         {
  226.             //wb.Save();
  227.             wb.Close(Type.Missing, Type.Missing, Type.Missing);
  228.             wbs.Close();
  229.             app.Quit();
  230.             wb = null;
  231.             wbs = null;
  232.             app = null;
  233.             GC.Collect();
  234.         }
  235.     }
  236. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

HI223.COM 举报邮箱:345306361@qq.com

GMT+8, 2025-7-3 18:55 , Processed in 0.063043 second(s), 18 queries .

Powered by HI223分享社区 2.3

© 2019-2020 Hi223 All Right Reserved

渝公网安备 50022502000358号

渝ICP备14008270号-3