c#.net 中 如何将gridview中的数据导出到excel中?
5个回答
展开全部
/// <summary>
/// 导出Datatable的数据到excel
/// </summary>
/// <param name="dt">datatable</param>
private void CreateExcel(DataTable dt)
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
resp.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
colHeaders += dt.Columns[1].Caption.Replace("ID", "编号") + "\t";
colHeaders += dt.Columns[2].Caption.Replace("FuncID", "操作名称") + "\t";
colHeaders += dt.Columns[0].Caption.Replace("logtypes", "操作类型") + "\t";
colHeaders += dt.Columns[6].Caption.Replace("LogDec", "操作内容") + "\t";
colHeaders += dt.Columns[5].Caption.Replace("UserID", "用户名称") + "\t";
colHeaders += dt.Columns[4].Caption.Replace("CreateTime", "操作时间") + "\n";
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < dt.Rows.Count; i++)
{
//if (i == (cl - 1))//最后一列,加n
ls_item += dt.Rows[i][1].ToString() + "\t";
ls_item += dt.Rows[i][2].ToString() + "\t";
ls_item += dt.Rows[i][0].ToString() + "\t";
ls_item += dt.Rows[i][6].ToString().Replace("\r\n", "") + "\t";
if (dt.Rows[i][5].ToString().Trim() == "")
{
ls_item += "无姓名" + "\t";
}
else
{
ls_item += dt.Rows[i][5].ToString() + "\t";
}
ls_item += dt.Rows[i][4].ToString() + "\n";
}
resp.Write(ls_item);
ls_item = "";
resp.End();
}
catch (Exception)
{
throw;
}
}
protected void btiiii_Click(object sender, EventArgs e)
{
try
{
DCExecl(Convert.ToInt32(this.txtstrPageindex.Value), Convert.ToInt32(this.txtendPageindex.Value));
new Facade.SystemLog().SaveSystemLogCode("系统日志",(int)Facade.EnumMuster.LOG_TYPE.DEVICE_OPERATION,Session["UserID"].ToString(),Session["UserID"].ToString()+"导出系统日志数据");
}
catch (Exception)
{
throw;
}
}
}
适当修改就行·
/// 导出Datatable的数据到excel
/// </summary>
/// <param name="dt">datatable</param>
private void CreateExcel(DataTable dt)
{
try
{
string fileName = DateTime.Now.ToString("yyyyMMddHHmm") + ".xls";
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
resp.ContentType = "application/ms-excel";
string colHeaders = "", ls_item = "";
//定义表对象与行对象,同时用DataSet对其值进行初始化
DataRow[] myRow = dt.Select();
int i = 0;
int cl = dt.Columns.Count;
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
colHeaders += dt.Columns[1].Caption.Replace("ID", "编号") + "\t";
colHeaders += dt.Columns[2].Caption.Replace("FuncID", "操作名称") + "\t";
colHeaders += dt.Columns[0].Caption.Replace("logtypes", "操作类型") + "\t";
colHeaders += dt.Columns[6].Caption.Replace("LogDec", "操作内容") + "\t";
colHeaders += dt.Columns[5].Caption.Replace("UserID", "用户名称") + "\t";
colHeaders += dt.Columns[4].Caption.Replace("CreateTime", "操作时间") + "\n";
resp.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < dt.Rows.Count; i++)
{
//if (i == (cl - 1))//最后一列,加n
ls_item += dt.Rows[i][1].ToString() + "\t";
ls_item += dt.Rows[i][2].ToString() + "\t";
ls_item += dt.Rows[i][0].ToString() + "\t";
ls_item += dt.Rows[i][6].ToString().Replace("\r\n", "") + "\t";
if (dt.Rows[i][5].ToString().Trim() == "")
{
ls_item += "无姓名" + "\t";
}
else
{
ls_item += dt.Rows[i][5].ToString() + "\t";
}
ls_item += dt.Rows[i][4].ToString() + "\n";
}
resp.Write(ls_item);
ls_item = "";
resp.End();
}
catch (Exception)
{
throw;
}
}
protected void btiiii_Click(object sender, EventArgs e)
{
try
{
DCExecl(Convert.ToInt32(this.txtstrPageindex.Value), Convert.ToInt32(this.txtendPageindex.Value));
new Facade.SystemLog().SaveSystemLogCode("系统日志",(int)Facade.EnumMuster.LOG_TYPE.DEVICE_OPERATION,Session["UserID"].ToString(),Session["UserID"].ToString()+"导出系统日志数据");
}
catch (Exception)
{
throw;
}
}
}
适当修改就行·
展开全部
你用google搜索 gridview excel,一堆的,百度有比较麻烦的回复审核机制,我就不贴了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//GridView重写
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
//导入Excel方法
private void ExportDataGrid(string FileType, string FileName)
{
Response.Charset = "UTF8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataGrid("application/ms-excel", "库存表.xlc");
}
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
//导入Excel方法
private void ExportDataGrid(string FileType, string FileName)
{
Response.Charset = "UTF8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8));
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
protected void Button3_Click(object sender, EventArgs e)
{
ExportDataGrid("application/ms-excel", "库存表.xlc");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上有很多源码的,可以自己搜一下啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询