C# 读取EXCEL文件的三种经典方法

using System;

using System.Data;using System.Configuration;

using System.Web;

using Microsoft.Office.Interop;

using Microsoft.Office.Core;

namespace Microsoft.Office.Interop.ExcelEdit{    

////// Microsoft.Office.Interop.ExcelEdit 的摘要说明

public class ExcelEdit   

 {    

    public string mFilename;    

    public Microsoft.Office.Interop.Excel.Application app;     

   public Microsoft.Office.Interop.Excel.Workbooks wbs;    

    public Microsoft.Office.Interop.Excel.Workbook wb;    

    public Microsoft.Office.Interop.Excel.Worksheets wss;     

   public Microsoft.Office.Interop.Excel.Worksheet ws;     

   public ExcelEdit()        

{            //            // TODO: 在此处添加构造函数逻辑            //        }    

    public void Create()//创建一个Microsoft.Office.Interop.Excel对象    

    {            

     app = new Microsoft.Office.Interop.Excel.Application();      

      wbs = app.Workbooks;       

     wb = wbs.Add(true);     

   }      

  public void Open(string FileName)//打开一个Microsoft.Office.Interop.Excel文件       

 {            

app = new Microsoft.Office.Interop.Excel.Application();           

 wbs = app.Workbooks;            

wb = wbs.Add(FileName);            

//wb = wbs.Open(FileName, 0, true, 5,"", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "t", false, false, 0, true,Type.Missing,Type.Missing);           

 //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);            

mFilename = FileName;       

 }        

public Microsoft.Office.Interop.Excel.Worksheet GetSheet(string SheetName)        //获取一个工作表      

  {            

Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName];           

 return s;    

    }     

   public Microsoft.Office.Interop.Excel.Worksheet AddSheet(string SheetName)        //添加一个工作表        

{          

  Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);           

 s.Name = SheetName;            

return s;       

 }       

 public void DelSheet(string SheetName)//删除一个工作表      

  {        

    ((Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[SheetName]).Delete();  

      }        

public Microsoft.Office.Interop.Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一     

   {            

Microsoft.Office.Interop.Excel.Worksheet s = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[OldSheetName];        

    s.Name = NewSheetName;         

   return s;       

 }        

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

 {            

Sheet.Name = NewSheetName;            

return Sheet;        

}       

 public void SetCellValue(Microsoft.Office.Interop.Excel.Worksheet ws, int x, int y, object value)        //ws:要设值的工作表    X行Y列    value  值       

 {           

 ws.Cells[x, y] = value;     

   }        

public void SetCellValue(string ws, int x, int y, object value)        //ws:要设值的工作表的名称 X行Y列 value 值      

  {            

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

  }        

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)        //设置一个单元格的属性  字体,  大小,颜色  ,对齐方式      

  {            

name = "宋体";           

 size = 12;            

color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;           

 HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;    

 ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;        

}       

 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)        

{           

 //name = "宋体";           

 //size = 12;           

 //color = Microsoft.Office.Interop.Excel.Constants.xlAutomatic;          

  //HorizontalAlignment = Microsoft.Office.Interop.Excel.Constants.xlRight;            Microsoft.Office.Interop.Excel.Worksheet ws = GetSheet(wsn);            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Name = name;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Size = size;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).Font.Color = color;            ws.get_Range(ws.Cells[Startx, Starty], ws.Cells[Endx, Endy]).HorizontalAlignment = HorizontalAlignment;       

 }      

  public void UniteCells(Microsoft.Office.Interop.Excel.Worksheet ws, int x1, int y1, int x2, int y2)        //合并单元格     

   {            

ws.get_Range(ws.Cells[x1, y1], ws.Cells[x2, y2]).Merge(Type.Missing);    

    }        

public void UniteCells(string ws, int x1, int y1, int x2, int y2)        //合并单元格       

 {            

GetSheet(ws).get_Range(GetSheet(ws).Cells[x1, y1], GetSheet(ws).Cells[x2, y2]).Merge(Type.Missing);   

     }       

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

 {            

for (int i = 0; i  <= dt.Rows.Count - 1; i++)       

    {             

   for (int j = 0; j  <= dt.Columns.Count - 1; j++)        

        {                    

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

          }        

    }      

  }       

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

 {           

 for (int i = 0; i  <= dt.Rows.Count - 1; i++)      

      {                

for (int j = 0; j  <= dt.Columns.Count - 1; j++)          

      {                   

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

   }           

 }      

  }        

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

 {           

 for (int i = 0; i  <= dt.Rows.Count - 1; i++)      

      {               

for (int j = 0; j  <= dt.Columns.Count - 1; j++)         

       {                   

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

     }         

   }       

 }        

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

  {          

  for (int i = 0; i  <= dt.Rows.Count - 1; i++)         

   {              

  for (int j = 0; j  <= dt.Columns.Count - 1; j++)             

   {                    

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

    }          

  }       

 }      

  public void InsertPictures(string Filename, string ws)        //插入图片操作一      

  {           

 GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);            //后面的数字表示位置      

  }      

  //public void InsertPictures(string Filename, string ws, int Height, int Width)        //插入图片操作二        

//{      

  //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);      

  //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;      

  //    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;        

//}       

 //public void InsertPictures(string Filename, string ws, int left, int top, int Height, int Width)        //插入图片操作三        

//{       

 //    GetSheet(ws).Shapes.AddPicture(Filename, MsoTriState.msoFalse, MsoTriState.msoTrue, 10, 10, 150, 150);       

 //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementLeft(left);       

 //    GetSheet(ws).Shapes.get_Range(Type.Missing).IncrementTop(top);     

   //    GetSheet(ws).Shapes.get_Range(Type.Missing).Height = Height;        

//    GetSheet(ws).Shapes.get_Range(Type.Missing).Width = Width;        

//}        

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)       

 //插入图表操作       

 {            

ChartDataType = Microsoft.Office.Interop.Excel.XlRowCol.xlColumns;            wb.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);        

    {                

wb.ActiveChart.ChartType = ChartType;   

             wb.ActiveChart.SetSourceData(GetSheet(ws).get_Range(GetSheet(ws).Cells[DataSourcesX1, DataSourcesY1], GetSheet(ws).Cells[DataSourcesX2, DataSourcesY2]), ChartDataType);     

           wb.ActiveChart.Location(Microsoft.Office.Interop.Excel.XlChartLocation.xlLocationAsObject, ws);          

  }      

  }     

   public bool Save()        //保存文档   

     {           

 if (mFilename == "")          

  {               

 return false;           

 }           

 else         

   {             

   try            

    {                    

wb.Save();                 

   return true;             

   }               

 catch (Exception ex)           

     {                    

return false;             

   }           

 }        

}       

 public bool SaveAs(object FileName)        //文档另存为       

 {            

try         

   {              

  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);     

           return true;          

  }            

catch (Exception ex)          

 {                

return false;      

      }     

   }      

  public void Close()        //关闭一个Microsoft.Office.Interop.Excel对象,销毁对象      

  {          

  //wb.Save();          

  wb.Close(Type.Missing, Type.Missing, Type.Missing);           

 wbs.Close();           

 app.Quit();          

  wb = null;            

wbs = null;            

app = null;            

GC.Collect();       

 }    

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,340评论 5 467
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,762评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,329评论 0 329
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,678评论 1 270
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,583评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,995评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,493评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,145评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,293评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,250评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,267评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,973评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,556评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,648评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,873评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,257评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,809评论 2 339

推荐阅读更多精彩内容

  • C#操作excel(多种方法比较) 我们在做excel资料的时候,通常有以下方法。 一.导入导出excel常用方法...
    北风知我意阅读 1,463评论 0 2
  • 原文来自:http://www.cnblogs.com/zhangqifeng/archive/2009/06/1...
    简单Liml阅读 1,166评论 0 0
  • 1.1 VBA是什么 直到90年代早期,使应用程序自动化还是充满挑战性的领域.对每个需要自动化的应用程序,人们不得...
    浮浮尘尘阅读 21,673评论 6 49
  • 本例为设置密码窗口 (1) If Application.InputBox(“请输入密码:”) = 1234 Th...
    浮浮尘尘阅读 13,564评论 1 20
  • 个人简介 王士生,字涵石,1947年出生于西安,天津市武清区籍贯。 现为:国家一级美术师,中国华夏...
    艺术范阅读 2,380评论 0 0