惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
T
Threatpost
人人都是产品经理
人人都是产品经理
大猫的无限游戏
大猫的无限游戏
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - Franky
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
M
MIT News - Artificial intelligence
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
S
Security Affairs
P
Proofpoint News Feed
L
LINUX DO - 最新话题
宝玉的分享
宝玉的分享
S
Security @ Cisco Blogs
H
Hacker News: Front Page
Security Archives - TechRepublic
Security Archives - TechRepublic
Vercel News
Vercel News
Engineering at Meta
Engineering at Meta
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
月光博客
月光博客
量子位
博客园_首页
The Last Watchdog
The Last Watchdog
D
DataBreaches.Net
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
The Register - Security
The Register - Security
Schneier on Security
Schneier on Security
H
Help Net Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
F
Full Disclosure
C
Cyber Attacks, Cyber Crime and Cyber Security
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
L
Lohrmann on Cybersecurity
S
Secure Thoughts
Stack Overflow Blog
Stack Overflow Blog
Cloudbric
Cloudbric
Microsoft Security Blog
Microsoft Security Blog

博客园 - 陈典洪

ASP.NET生成Excel文件的历程总结 SharePoint 2010 安装部署 用VSTA动态改变infopath数据源查询参数与反回查询结果 将excel2003转换为excel2007格式 用VS2010为SharePoint 2010 添加Ribbon自定义按钮 在sql server 中如何移动tempdb到新的位置 禁用MOSS2007“我的网站”功能 使用Exchange 2007搭建多域名邮件系统 Exchange 2007 配置 如何简化Exchange 2007 OWA URL访问 [转载]利用MOSS文档库自制一个山寨版mp3在线播放器 - 陈典洪 - 博客园 MOSS中自定义WebService与客户端应用 将项目从vs2008转到Vs2005的办法 如何控制列表视图栏位的宽度? WSS(MOSS)如何修改Rich文本编辑器的宽度 WSS 3.0 and MOSS 2007 SDK 更新到了1.5版本啦 moss 自定义文档库文档图标 - 陈典洪 - 博客园 moss 列表类型和对应类型编号 MOSS LIST的一些属性说明
C# Excel操作类
陈典洪 · 2010-11-24 · via 博客园 - 陈典洪

经常碰到需要操作Excel的情况,特别是涉及到DataTable和GridView之类东东的时候,导入导出Excel,并定制样式,调整字段等等的操作就成了家常便饭.

//引入Excel的COM组件

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop;
using Microsoft.Office.Core;namespace ExcelEdit
{
    
/// <summary>
    
/// ExcelEdit 的摘要说明
    
/// </summary>
    public class ExcelEdit
    {
        
public string mFilename;
        
public Excel.Application app;
        
public Excel.Workbooks wbs;
        
public Excel.Workbook wb;
        
public Excel.Worksheets wss;
        
public Excel.Worksheet ws;
        
public ExcelEdit()
        {
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }
        
public void Create()//创建一个Excel对象
        {
            app 
= new Excel.Application();
            wbs 
= app.Workbooks;
            wb 
= wbs.Add(true);
        }
        
public void Open(string FileName)//打开一个Excel文件
        {
            app 
= new Excel.Application();
            wbs 
= app.Workbooks;
            wb 
= wbs.Add(FileName);
            
//wb = wbs.Open(FileName,  0, true, 5,"", "", true, 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,Excel.XlPlatform.xlWindows,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
            mFilename = FileName;
        }
        
public Excel.Worksheet GetSheet(string SheetName)//获取一个工作表
        {
            Excel.Worksheet s 
= (Excel.Worksheet)wb.Worksheets[SheetName];
            
return s;
        }
        
public Excel.Worksheet AddSheet(string SheetName)//添加一个工作表
        {
            Excel.Worksheet s 
= (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            s.Name 
= SheetName;
            
return s;
        }
public void DelSheet(string SheetName)//删除一个工作表
        {
            ((Excel.Worksheet)wb.Worksheets[SheetName]).Delete();
        }
        
public Excel.Worksheet ReNameSheet(string OldSheetName, string NewSheetName)//重命名一个工作表一
        {
            Excel.Worksheet s 
= (Excel.Worksheet)wb.Worksheets[OldSheetName];
            s.Name 
= NewSheetName;
            
return s;
        }
public Excel.Worksheet ReNameSheet(Excel.Worksheet Sheet, string NewSheetName)//重命名一个工作表二
        {

            Sheet.Name 

= NewSheetName;return Sheet;
        }
public void SetCellValue(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(Excel.Worksheet ws, int Startx, int Starty, int Endx, int Endy, int size, string name, Excel.Constants color, Excel.Constants HorizontalAlignment)//设置一个单元格的属性   字体,   大小,颜色   ,对齐方式
        {
            name 
= "宋体";
            size 
= 12;
            color 
= Excel.Constants.xlAutomatic;
            HorizontalAlignment 
= 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, Excel.Constants color, Excel.Constants HorizontalAlignment)
        {
            
//name = "宋体";
            
//size = 12;
            
//color = Excel.Constants.xlAutomatic;
            
//HorizontalAlignment = Excel.Constants.xlRight;

            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(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)//将内存中数据表格插入到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, Excel.Worksheet ws, int startX, int startY)//将内存中数据表格插入到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)//将内存中数据表格添加到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, Excel.Worksheet ws, int startX, int startY)//将内存中数据表格添加到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, 
1010150150);//后面的数字表示位置
        }//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(Excel.XlChartType ChartType, string ws, int DataSourcesX1, int DataSourcesY1, int DataSourcesX2, int DataSourcesY2, Excel.XlRowCol ChartDataType)//插入图表操作
        {
            ChartDataType 
= 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(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, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                
return true;

            }

catch (Exception ex)
            {
                
return false;

            }
        }

public void Close()//关闭一个Excel对象,销毁对象
        {
            
//wb.Save();
            wb.Close(Type.Missing, Type.Missing, Type.Missing);
            wbs.Close();
            app.Quit();
            wb 
= null;
            wbs 
= null;
            app 
= null;
            GC.Collect();
        }
    }
}