我想用C#读取一个Excel模板,然后将从库中计算出的数据填写到模板的指定位置上。我从CSDN上看了一些关于这类问题的帖子,可是照那些方法都行不同,比如
Excel.Application xlApp = new Excel.Application();
xlApp.Visible=true;
等等很多的方法报错或提示没有这个方法。我想请教一下有哪位朋友真正操作成功了的,请帮个忙,先谢谢了!
另外,好像不同的Excel版本要引用不同的控件,这个在程序里怎样做,怎样实现?谢谢!
Excel.Application xlApp = new Excel.Application();
xlApp.Visible=true;
等等很多的方法报错或提示没有这个方法。我想请教一下有哪位朋友真正操作成功了的,请帮个忙,先谢谢了!
另外,好像不同的Excel版本要引用不同的控件,这个在程序里怎样做,怎样实现?谢谢!
解决方案 »
- 纠结还是IO的问题。...写个代码注入真麻烦。
- 新人问一个愚笨的问题,实在不知道从哪下手,语法方面的
- c#中的datagridview列顺序问题?
- 怎样隐藏Panel的滚动条(Forms)?
- C# datagrid 捕获键盘事件
- 泛型类继承UITypeEditor后,无效
- C#webbrower中用AppendChild添加元素不显示样式
- 经过包装的COM对象(COM Interop)能否存入数据库的字段中
- 能推荐一本详细介绍.net对象的书吗?全当字典用的那种!
- 关于J#.net的问题(不是C#)
- 正则--怎样匹配“//” 与 回车 (只有10分了)
- 部署打包程序,如何实现默认目录的安装,也就是无法选择安装目录!
选择Microsoft Excel 9.0 Object Library(EXCEL2000),然后选择"确定"。表示在工程
中要引用EXCEL类型库。
选择Microsoft Excel 11.0 Object Library(EXCEL2003),然后选择"确定"。表示在工程
中要引用EXCEL类型库。
你有成功例子吗?请发出来吧,谢谢!
我写的代码如下,用的也是excel2000,你看下哈
string strMBfile = "",strNewfile = "",strFilePath="",strMBFilePath = "",strNewFilePath="",str_FileUrl="";
strFilePath = Request.ServerVariables.Get("PATH_TRANSLATED");
str_FileUrl = Request.Url.ToString();
strMBFilePath = strFilePath.Substring(0,strFilePath.LastIndexOf("\\"));
strMBfile = strMBFilePath + "\\excel模版.xls";
if(!File.Exists(strMBfile))
{Alert.Show("EXCEL模版不存在或路径不正确!");}
Random nRandom = new Random(DateTime.Now.Millisecond);
strNewFilePath = nRandom.NextDouble()+ ".xls";
strNewfile = strMBFilePath + "\\temp\\" + strNewFilePath;
if(File.Exists(strNewfile))
{ File.Delete(strNewfile); }
File.Copy(strMBfile,strNewfile);
#region//往模板写数据
Excel.Application myApp;
Excel.Workbook myBook;
Excel.Worksheet mySheet;
Excel.Sheets mySheets;
myApp=null; myBook = null; mySheet = null;
object oMissiong = System.Reflection.Missing.Value;
myApp = new Excel.ApplicationClass();
myApp.Visible=false;
myApp.Workbooks.Open(strNewfile.Replace(".txt",".xls"),oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
myBook = myApp.Workbooks[1];
mySheets = myBook.Worksheets;
mySheet = (Excel.Worksheet)mySheets[1];
mySheet.Activate();
//开始写如数据
mySheet.Cells[1,1]= str_FileUrl.Substring(0,str_FileUrl.LastIndexOf("/")) + "/temp/" + strNewFilePath.Replace(".xls",".txt");
myBook.Save();
myBook.Close(true,strNewfile,true);
这里可以打操作excel基本上所的功能,如列宽,行宽等,具体的调用excel的那些函数你最好看下excel的vba帮助。这个最大的问题在于如果写入的数据太大,就不行了哈,时间太长了!
Excel.Application myApp=null;
Workbook myBook=null;
Worksheet mySheet=null;
myApp= new ApplicationClass();
myApp.Visible=false;
object oMissiong=System.Reflection.Missing.Value;
myBook=myApp.Workbooks.Add(true);
mySheet=(Worksheet)myBook.ActiveSheet;
mySheet.Cells.Select();
mySheet.Cells.NumberFormatLocal = "@"; //写进表格中的数据
//
mySheet.Cells[1,1]="工单号";
mySheet.Cells[1,2]="品牌";
.............
我引入的是:Microsoft Excel 11.0 Object Library(EXCEL2003),你申明的时候写出名字空间全路径就应该没有问题了:private Microsoft.Office.Interop.Excel.Application myExcel1;
private Microsoft.Office.Interop.Excel.Workbook myWorkbook1;
private Microsoft.Office.Interop.Excel.Worksheet mySheets1;this.myExcel1=new Microsoft.Office.Interop.Excel.ApplicationClass();
this.myWorkbook1=this.myExcel1.Workbooks.Add(1);
this.mySheets1=(Microsoft.Office.Interop.Excel.Worksheet)this.myWorkbook1.Sheets[1];for(int i=0;i<tb1.Columns.Count;i++)
{
this.mySheets1.Cells[1,i+1]=tb1.Columns[i].ColumnName;
}
for(int i=0;i<tb1.Rows.Count;i++)
{
for(int j=0;j<tb1.Columns.Count;j++)
{
this.mySheets1.Cells[i+2,j+1]=tb1.Rows[i][j].ToString();
}
}
this.myExcel1.Visible=true;
程序运行到这一句的时候会报错:
拒绝访问。
异常详细信息: System.UnauthorizedAccessException: 拒绝访问。 请问怎么解决?
Excel._Workbook objBook;
Excel.Workbooks objBooks;
Excel._Worksheet objSheet;objApp = new Excel.ApplicationClass();objBooks = objApp.Workbooks;
objBook = objBooks.Add(System.Reflection.Missing.Value);objSheet = (Excel._Worksheet)objBook.Sheets.get_Item(1);
我编译的时候,提示如下:
d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(30): “Excel.Application”并不包含对“Visible”的定义
d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(31): “Excel.Application”并不包含对“Workbooks”的定义
d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(32): “Excel.Application”并不包含对“Workbooks”的定义
d:\inetpub\wwwroot\ExcelDemo\outExcel.aspx.cs(33): “Excel.Workbook”并不包含对“Worksheets”的定义