温故知新~~~发布Excel操作类C#版的

Vince520 2007-07-29 11:48:27
//引入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];

}
}

}

...全文
2823 103 打赏 收藏 转发到动态 举报
写回复
用AI写文章
103 条回复
切换为时间正序
请发表友善的回复…
发表回复
Brian_wh 2009-11-06
  • 打赏
  • 举报
回复
mark
kevin_水滴石穿 2009-11-06
  • 打赏
  • 举报
回复
谢谢了
xiaotou1256 2009-02-24
  • 打赏
  • 举报
回复
veiny 2008-11-13
  • 打赏
  • 举报
回复
mark
on_my_way20xx 2008-11-13
  • 打赏
  • 举报
回复
mark 之....
ljleager 2008-05-22
  • 打赏
  • 举报
回复
收藏
blackbrod 2008-03-20
  • 打赏
  • 举报
回复
thank you
zhoude3 2008-03-14
  • 打赏
  • 举报
回复
不错的代码!
菜鸟来了 2008-02-23
  • 打赏
  • 举报
回复
mark
wuyi8808 2008-02-23
  • 打赏
  • 举报
回复
mark
software_ant 2008-02-23
  • 打赏
  • 举报
回复
mark
nmlidong 2008-02-23
  • 打赏
  • 举报
回复
remark
angleoldhen 2008-02-23
  • 打赏
  • 举报
回复
mark
vefo 2007-12-09
  • 打赏
  • 举报
回复
mark
补课 2007-12-09
  • 打赏
  • 举报
回复
真是太感谢了...
hotlu1982 2007-12-04
  • 打赏
  • 举报
回复
mark
zjsyw 2007-12-04
  • 打赏
  • 举报
回复
gz
SZSEAWIND 2007-10-31
  • 打赏
  • 举报
回复
强人。
wuhq030710914 2007-10-31
  • 打赏
  • 举报
回复
d
zxf92183 2007-10-31
  • 打赏
  • 举报
回复
收了,jf
加载更多回复(80)

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧