我导出excel的时候总是出错,编写的语言是C#(.net2005)
提示的报错如下:
未能加载文件或程序集"CarlosAg.ExcelXMLWriter, Version=1.0.0.6,Culture=neutral,PublicKeyToken=eac822d9c4196ba1"
或他的某一个依赖项,系统找不到指定的文件。不知道我这个是引用缺少了一些还是我的代码本身就不对,
还有就是于CarlosAg.ExcelXMLWriter相关的引用都有哪些?我的引用是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;using System.Collections;
using System.Runtime.InteropServices;
using System.IO;我的关于导出excel那段的代码是:
private void button1_Click(object sender, EventArgs e)
{
this.boundGridControl1.ExportToExcel("temp.xls", true, true, true);
System.Diagnostics.Process.Start("excel.exe");
}
这段代码执行的时候报错就是上边的那段,如果是到出道txt文档就不会有问题
private void button1_Click(object sender, EventArgs e)
{
this.boundGridControl1.ExportToTabDelimitedText("temp.txt", true, true, true);
System.Diagnostics.Process.Start("notepad.exe", "temp.txt"); //用记事本
}
提示的报错如下:
未能加载文件或程序集"CarlosAg.ExcelXMLWriter, Version=1.0.0.6,Culture=neutral,PublicKeyToken=eac822d9c4196ba1"
或他的某一个依赖项,系统找不到指定的文件。不知道我这个是引用缺少了一些还是我的代码本身就不对,
还有就是于CarlosAg.ExcelXMLWriter相关的引用都有哪些?我的引用是:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ZedGraph;using System.Collections;
using System.Runtime.InteropServices;
using System.IO;我的关于导出excel那段的代码是:
private void button1_Click(object sender, EventArgs e)
{
this.boundGridControl1.ExportToExcel("temp.xls", true, true, true);
System.Diagnostics.Process.Start("excel.exe");
}
这段代码执行的时候报错就是上边的那段,如果是到出道txt文档就不会有问题
private void button1_Click(object sender, EventArgs e)
{
this.boundGridControl1.ExportToTabDelimitedText("temp.txt", true, true, true);
System.Diagnostics.Process.Start("notepad.exe", "temp.txt"); //用记事本
}
解决方案 »
- 一个数据库ED,让ED“分离”和“脱机”有什么区别,这两个SQL语句如何写
- 一个关于鼠标离开事件的处理
- 旋转角度问题
- 想请问一下:要想用C#写出 用类似于进度条的东西表示速度弄怎么弄啊
- 取得bmp图片的颜色
- 用c#判断矩形
- 请问怎样加密长字符串(0-2000字符左右)
- ms speech sdk 5.1 开发语音客户端软件,如何部署到windows2000操作系统的客户端(急,在线等)
- 在vs中如何新建接口啊
- 版主----以前的很多帖子都无法显示----
- 多线程非要同步吗?同步是保证一个线程处理一块代码,只允许我这线程进行..但是我不lock不行吗?会有什么问题呢?在线等!
- 请问 如何在cs文件中弹出对话框 然后根据用户选择 执行不同的数据库操作?
using CarlosAg.ExcelXmlWriter;
CarlosAg.ExcelXmlWriter是第三方dll控件,可以到http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx下载需要在工程里添加引用,并在代码加using CarlosAg.ExcelXmlWriter;
添加引用 using CarlosAg.ExcelXmlWriter;
/// 将datatable中的数据导出到指定的excel文件中
/// </summary>
/// <param name="page">web页面对象</param>
/// <param name="tab">包含被导出数据的datatable对象</param>
/// <param name="filename">excel文件的名称</param>
public static void Export(System.Web.UI.Page page, DataTable tab, string filename)
{
System.Web.HttpResponse httpresponse = page.Response;
System.Web.UI.WebControls.DataGrid datagrid = new DataGrid();
datagrid.DataSource = tab.DefaultView;
datagrid.AllowPaging = false;
datagrid.HeaderStyle.BackColor = Color.White;
datagrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
datagrid.HeaderStyle.Font.Bold = true;
datagrid.DataBind();
httpresponse.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename,System.Text.Encoding.UTF8)); //filename="*.xls";
httpresponse.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
httpresponse.ContentType = "application/ms-excel";
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
datagrid.RenderControl(hw); string filepath = page.Server.MapPath("..") + "\\files\\" + filename;
StreamWriter sw = System.IO.File.CreateText(filepath);
sw.Write(tw.ToString());
sw.Close(); DownFile(httpresponse, filename, filepath); httpresponse.End();
}
private static bool DownFile(System.Web.HttpResponse response, string filename, string fullpath)
{
try
{
response.ContentType = "application/octet-stream"; response.AppendHeader("content-disposition", "attachment;filename=" +
HttpUtility.UrlEncode(filename, Encoding.UTF8) + ";charset=gb2312");
System.IO.FileStream fs = System.IO.File.OpenRead(fullpath);
long flen = fs.Length;
int size = 102400;//每100k同时下载数据
byte[] readdata = new byte[size];//指定缓冲区的大小
if (size > flen) size = Convert.ToInt32(flen);
long fpos = 0;
bool isend = false;
while (!isend)
{
if ((fpos + size) > flen)
{
size = Convert.ToInt32(flen - fpos);
readdata = new byte[size];
isend = true;
}
fs.Read(readdata, 0, size);//读入一个压缩块
response.BinaryWrite(readdata);
fpos += size;
}
fs.Close();
System.IO.File.Delete(fullpath);
return true;
}
catch
{
return false;
}
}
你要用TlbImp.exe 转换工具 把Ocx 把它转换成dll
目前知道有2种方法:第一种:用流保存成xls文件. 这种方法比较好,不用引用Excel组件. 下面是本人使用具体例子,可以参考 using System.IO; /// <summary> /// 另存新档按钮 /// </summary> private void SaveAs() //另存新档按钮 导出成Excel