首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 网络编程-> Perl教程
C#中使用Excel-.NET教程,C#语言
作者:网友供稿 点击:353
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
在做一个小项目,需要把一些查询结果导出到excel,找了一些资料,自己也总结出了一点方法,与大家共享。

一、首先简要描述一下如何操作excel表


先要添加对excel的引用。选择项目-〉添加引用-〉com-〉添加microsoft excel 9.0。(不同的office讲会有不同版本的dll文件)。
using excel;
using system.reflection;

//产生一个excel.application的新进程
excel.application app = new excel.application();
if (app == null)
{
statusbar1.text = "error: excel couldnt be started!";
return ;
}

app.visible = true; //如果只想用程序控制该excel而不想让用户操作时候,可以设置为false
app.usercontrol = true;

workbooks workbooks =app.workbooks;

_workbook workbook = workbooks.add(xlwbatemplate.xlwbatworksheet); //根据模板产生新的workbook
// _workbook workbook = workbooks.add("c:\\a.xls"); //或者根据绝对路径打开工作簿文件a.xls


sheets sheets = workbook.worksheets;
_worksheet worksheet = (_worksheet) sheets.get_item(1);
if (worksheet == null)
{
statusbar1.text = "error: worksheet == null";
return;
}


// this paragraph puts the value 5 to the cell g1
range range1 = worksheet.get_range("a1", missing.value);
if (range1 == null)
{
statusbar1.text = "error: range == null";
return;
}
const int ncells = 2345;
range1.value2 = ncells;



二、示例程序


在visual studio .net中建立一个c# winform工程.
添加microsoft excel object library引用:
右键单击project , 选“添加引用”
在com 标签项,选中 locate microsoft excel object library
点确定按钮完成添加引用。 on the view menu, select toolbox to display the toolbox. add two buttons and a check box to form1.
在form1上添加一个button1,双击 button1,添加click事件的代码.把数组里的数据填到excel表格。
首先添加引用:

using system.reflection;
using excel = microsoft.office.interop.excel;


声明两个类的成员变量
excel.application objapp;
excel._workbook objbook;

private void button1_click(object sender, system.eventargs e)
{
excel.workbooks objbooks;
excel.sheets objsheets;
excel._worksheet objsheet;
excel.range range;

try
{
// instantiate excel and start a new workbook.
objapp = new excel.application();
objbooks = objapp.workbooks;
objbook = objbooks.add( missing.value );
objsheets = objbook.worksheets;
objsheet = (excel._worksheet)objsheets.get_item(1);

//get the range where the starting cell has the address
//m_sstartingcell and its dimensions are m_inumrows x m_inumcols.
range = objsheet.get_range("a1", missing.value);
range = range.get_resize(5, 5);

if (this.fillwithstrings.checked == false)
{
//create an array.
double[,] saret = new double[5, 5];

//fill the array.
for (long irow = 0; irow < 5; irow++)
{
for (long icol = 0; icol < 5; icol++)
{
//put a counter in the cell.
saret[irow, icol] = irow * icol;
}
}

//set the range value to the array.
range.set_value(missing.value, saret );
}

else
{
//create an array.
string[,] saret = new string[5, 5];

//fill the array.
for (long irow = 0; irow < 5; irow++)
{
for (long icol = 0; icol < 5; icol++)
{
//put the row and column address in the cell.
saret[irow, icol] = irow.tostring() + "|" + icol.tostring();
}
}

//set the range value to the array.
range.set_value(missing.value, saret );
}

//return control of excel to the user.
objapp.visible = true;
objapp.usercontrol = true;
}
catch( exception theexception )
{
string errormessage;
errormessage = "error: ";
errormessage = string.concat( errormessage, theexception.message );
errormessage = string.concat( errormessage, " line: " );
errormessage = string.concat( errormessage, theexception.source );

messagebox.show( errormessage, "error" );
}
}

4.在form1上添加一个button2,双击 button2,添加click事件的代码,从excel表格读数据到数组:

private void button2_click(object sender, system.eventargs e)
{
excel.sheets objsheets;
excel._worksheet objsheet;
excel.range range;

try
{
try
{
//get a reference to the first sheet of the workbook.
objsheets = objbook.worksheets;
objsheet = (excel._worksheet)objsheets.get_item(1);
}

catch( exception theexception )
{
string errormessage;
errormessage = "cant find the excel workbook. try clicking button1 " +
"to create an excel workbook with data before running button2.";

messagebox.show( errormessage, "missing workbook?");

//you cant automate excel if you cant find the data you created, so
//leave the subroutine.
return;
}

//get a range of data.
range = objsheet.get_range("a1", "e5");

//retrieve the data from the range.
object[,] saret;
saret = (system.object[,])range.get_value( missing.value );

//determine the dimensions of the array.
long irows;
long icols;
irows = saret.getupperbound(0);
icols = saret.getupperbound(1);

//build a string that contains the data of the array.
string valuestring;
valuestring = "array data\n";

for (long rowcounter = 1; rowcounter <= irows; rowcounter++)
{
for (long colcounter = 1; colcounter <= icols; colcounter++)
{

//write the next value into the string.
valuestring = string.concat(valuestring,
saret[rowcounter, colcounter].tostring() + ", ");
}

//write in a new line.
valuestring = string.concat(valuestring, "\n");
}

//report the value of the array.
messagebox.show(valuestring, "array values");
}

catch( exception theexception )
{
string errormessage;
errormessage = "error: ";
errormessage = string.concat( errormessage, theexception.message );
errormessage = string.concat( errormessage, " line: " );
errormessage = string.concat( errormessage, theexception.source );

messagebox.show( errormessage, "error" );
}
}

三、更多内容
《how to: transfer data to an excel workbook by using visual c# .net》描述了多种方式(如数组、数据集、ado.net、xml)把数据导到excel表格的方法。

如果你需要把大数据量倒入到excel 表的话,建议使用 clipboard(剪贴板)的方法。实现方法参看上面的连接,讨论参看:http://expert.csdn.net/expert/topic/3086/3086690.xml

倒完数据后,在程序退出之前,如果需要结束excel 的进程,讨论参看:http://expert.csdn.net/expert/topic/3068/3068466.xml
讨论的结果就是:提前垃圾回收,或者杀死进程。




文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·无刷新聊天室(短信陪聊程序)-ASP教程,ASP应用
·C#中使用Excel-.NET教程,C#语言
·简要JAVA数据类型转换-JSP教程,Java技巧及代码
·Recordset对象方法详解-ASP教程,ASP应用
·选用和配置ups时应注意的几个问题
·新型无源元件的现状与发展
·用正则表达式解析C#文件(updated)-.NET教程,C#语言
·正则表达式大全-ASP教程,ASP应用
·VB与ActiveX控件签名谈-.NET教程,VB.Net语言
·IIS6 和Tomcat5 的整合-ASP教程,ASP应用
最新文章
·网上能免费赚钱?想要网上创业吗?_网赚技巧
·孙中伟:聚合文学网站资源的功能_站长访谈
·网站最令人讨厌的用户体验_站长心得
·设计理论:谈用户体验,别落下商业利益_站长心得
·经营个人网站需要脚踏实地_站长心得
·google adwords关键词广告须注意的7个问题_google推广
·网站提交google注册应注意的问题_google推广
·伦敦mecompany网站设计师谈网页布局艺术2_站长心得
·搜索引擎不收录页面的常见原因_seo网站优化
·搜索引擎注册九大秘法_站长心得
相关主题
  • c#中使用存储过程中的返回值_c#应用
  • c#中使用textbox控件的两个问题_c#应用
  • c#中使用net share命令时要注意的问题_c#应用
  • c#中使用dts来导入数据及相关问题 _c#应用
  • c#中使用多线程二-.NET教程,C#语言
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭