网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
11月17日漏签0天
c#吧 关注:188,642贴子:823,632
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 6回复贴,共1页
<<返回c#吧
>0< 加载中...

导出excel的问题,来个高手帮下忙!

  • 只看楼主
  • 收藏

  • 回复
  • haostyle
  • c#大菜鸟
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
我网上找了段代码。但是效果不好。


  • haostyle
  • c#大菜鸟
    4
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public void CreateExcel(DataSet ds, string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
string colHeaders = "", ls_item = ""; DataTable dt = ds.Tables[0];
int i = 0;
int cl = dt.Columns.Count; for (i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
colHeaders += dt.Columns[i].Caption.ToString() + "/n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "/t";
} }
resp.Write(colHeaders);
foreach (DataRow row in dt.Rows)
{
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))
{
ls_item += row[i].ToString() + "/n";
}
else
{
ls_item += row[i].ToString() + "/t";
} }
resp.Write(ls_item);
ls_item = "";
}
resp.End();
}


2025-11-17 18:32:44
广告
不感兴趣
开通SVIP免广告
  • ChobitsSP
  • c#诠释者
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个是直接用字符串写的 没有格式也没有过滤特殊符号 当然不稳定


  • ◇蝴蝶ぢ
  • c#大牛
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
推荐使用NPOI


  • ChobitsSP
  • c#诠释者
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
http://blog.csdn.net/fangxinggood/article/details/329253


  • 清风3keke
  • c#攻城狮
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
using System;
using System.Collections.Generic;
using System.Text;
using System;
using System.Web; namespace BHSJJBizlogic
{
/**/
/// <summary>
/// 彭建军
/// 导出报表数据存入word或execl文件
/// 2008-06-19
/// </summary>
public class ExportData
{
#region 构造函数
public ExportData()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#endregion /**/
/// <summary>
/// 将Web控件或页面信息导出(不带文件名参数)
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="DocumentType">导出类型:Excel或Word</param>
public void ExportControl(System.Web.UI.Control source, string DocumentType)
{
//设置Http的头信息,编码格式
if (DocumentType == "Excel")
{
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
} else if (DocumentType == "Word")
{
//Word
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载文件.doc", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-word";
} HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态
source.Page.EnableViewState = false; //初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter); //输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
} /**/
/// <summary>
/// 将Web控件或页面信息导出(带文件名参数)
/// </summary>
/// <param name="source">控件实例</param>
/// <param name="DocumentType">导出类型:Excel或Word</param>
/// <param name="filename">保存文件名</param>
public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
{
//设置Http的头信息,编码格式
if (DocumentType == "Excel")
{
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".xls", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
} else if (DocumentType == "Word")
{
//Word
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename + ".doc", System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-word";
} HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; //关闭控件的视图状态
source.Page.EnableViewState = false; //初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter); //输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
} #region
//方法ExportControl(System.Web.UI.Control source, string DocumentType,string filename)中
//第一个参数source表示导出的页面或控件名,当为datagrid或dataList控件时,在导出Excel/word文件时,必须把控件的分页、排序等属性去除并重新绑定,
//第二个参数DocumentType表示导出的文件类型word或excel
//第三个参数filename表示需要导出的文件所取的文件名
//调用方法:
//ExportData export=new ExportData();
//export.ExportControl(this, "Word","testfilename");//当为this时表示当前页面
//这是将整个页面导出为Word,并命名为testfilename
#endregion
}
}



登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 6回复贴,共1页
<<返回c#吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示