C# excel 报表制作 我是新手 公司让做个C#的 excel报表网上的例子很多 可是我打开VS还是不知道如何开始 excel是用类 还是窗体的进行编写呢求教知情者最好带 excel报表 简单功能开发 代码 谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Excel操作类 /// <summary> /// 数据导出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnDataExport_Click(object sender, EventArgs e) { try { this.Response.Clear(); this.Response.Buffer = true; this.Response.Charset = "GB2312"; //page.Response.Charset = "UTF-8"; //文件名称 string fileName = ""; //返回的数据 DataSet ds = null; //时间 string time = this.txtTime.Text.Trim() == "" ? DateTime.Now.ToString("yyyy-MM-dd") : this.txtTime.Text.Trim(); if (this.rdoHttp.Checked == true) { fileName = "http"; ds = DC9000.BLL.DataCenter.GetHttpList(time); } else if (this.rdoPost.Checked == true) { fileName = "post"; ds = DC9000.BLL.DataCenter.GetPostList(time); } Response.ClearHeaders(); this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName +" "+ time + " .xls"); //this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls"); this.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文 this.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 this.EnableViewState = false; Response.Write(ExportTable(ds)); Response.End(); } catch (Exception) { } finally { } } /// <summary> /// 读取数据 /// </summary> /// <param name="ds"></param> /// <returns></returns> public static string ExportTable(DataSet ds) { try { StringBuilder sb = new StringBuilder(); int count = 0; foreach (DataTable tb in ds.Tables) { sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">"); sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">"); //写出列名 sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">"); foreach (DataColumn column in tb.Columns) { sb.AppendLine("<td>" + column.ColumnName + "</td>"); } sb.AppendLine("</tr>"); //写出数据 foreach (DataRow row in tb.Rows) { sb.Append("<tr>"); foreach (DataColumn column in tb.Columns) { //if (column.ColumnName.Equals("证件编号") || column.ColumnName.Equals("报名编号")) // sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>"); //else sb.Append("<td>" + row[column].ToString() + "</td>"); } sb.AppendLine("</tr>"); count++; } sb.AppendLine("</table>"); } return sb.ToString(); } catch (Exception) { return ""; } finally { } } 这个功能类和窗体都应该使用到,需要准备以下知识或工具:1、VSTO excel文档级的程序知识如一楼提到的excel操作类;2、如果软件要求在应用程序中嵌入excel文档请使用DSOFramer控件,这是微软发布的(直接在百度里能找到源代码和案例);3、如果软件要求用户的操作是在Office应用程序里操作,请使用excel外接程序的方式进行,这样出来的操作嵌在excel应用程序中;个人认为准备以上内容应该可以完成你的任务,因为我已经完成了。 水晶报表bobj未定义的错误???? 那些条条框框真的重要么?反编码规范的做法的思考 请各位帮忙看一下下咯,谢谢啦 关于datagridview分页 大家好,我问个安装包的 遇到个问题,急 后台线程监控WEB页面如何实现 datagridview 单元格动态输入问题请教! C#里怎么实现 CreateObject ByteFX在Linux访问Mysql的乱码问题!! 大家推举基本.net方面的测试书籍(基于vs.net测试项目的) C#开发魅族M8应用?
/// 数据导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDataExport_Click(object sender, EventArgs e)
{
try
{
this.Response.Clear();
this.Response.Buffer = true;
this.Response.Charset = "GB2312";
//page.Response.Charset = "UTF-8"; //文件名称
string fileName = "";
//返回的数据
DataSet ds = null;
//时间
string time = this.txtTime.Text.Trim() == "" ? DateTime.Now.ToString("yyyy-MM-dd") : this.txtTime.Text.Trim(); if (this.rdoHttp.Checked == true)
{
fileName = "http";
ds = DC9000.BLL.DataCenter.GetHttpList(time);
}
else if (this.rdoPost.Checked == true)
{
fileName = "post";
ds = DC9000.BLL.DataCenter.GetPostList(time);
}
Response.ClearHeaders();
this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName +" "+ time + " .xls");
//this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + System.DateTime.Now.ToString("_yyMMdd_hhmm") + ".xls");
this.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
this.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
Response.Write(ExportTable(ds));
Response.End();
}
catch (Exception)
{ }
finally
{ }
} /// <summary>
/// 读取数据
/// </summary>
/// <param name="ds"></param>
/// <returns></returns>
public static string ExportTable(DataSet ds)
{
try
{
StringBuilder sb = new StringBuilder();
int count = 0; foreach (DataTable tb in ds.Tables)
{
sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
sb.AppendLine("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");
//写出列名
sb.AppendLine("<tr style=\"font-weight: bold; white-space: nowrap;\">");
foreach (DataColumn column in tb.Columns)
{
sb.AppendLine("<td>" + column.ColumnName + "</td>");
}
sb.AppendLine("</tr>"); //写出数据
foreach (DataRow row in tb.Rows)
{
sb.Append("<tr>");
foreach (DataColumn column in tb.Columns)
{
//if (column.ColumnName.Equals("证件编号") || column.ColumnName.Equals("报名编号"))
// sb.Append("<td style=\"vnd.ms-excel.numberformat:@\">" + row[column].ToString() + "</td>");
//else
sb.Append("<td>" + row[column].ToString() + "</td>");
}
sb.AppendLine("</tr>");
count++;
}
sb.AppendLine("</table>");
} return sb.ToString();
}
catch (Exception)
{
return "";
}
finally
{ }
}
1、VSTO excel文档级的程序知识如一楼提到的excel操作类;
2、如果软件要求在应用程序中嵌入excel文档请使用DSOFramer控件,这是微软发布的(直接在百度里能找到源代码和案例);
3、如果软件要求用户的操作是在Office应用程序里操作,请使用excel外接程序的方式进行,这样出来的操作嵌在excel应用程序中;
个人认为准备以上内容应该可以完成你的任务,因为我已经完成了。