C#导入excel的问题 WebForm中,要把sqlServer的信息导入到excel中,应该怎样实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 protected void btnExport_Click(object sender, EventArgs e) { Response.Clear(); Response.Buffer = true; //Response.Charset = "GB2312"; Response.AppendHeader("Content-Disposition", "attachment;filename=Report.xls"); //Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/ms-excel"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); this.divResult.RenderControl(oHtmlTextWriter); Response.Output.Write(oStringWriter.ToString()); Response.Flush(); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { } public override void VerifyRenderingInServerForm(Control control) { }这一段代码是执行什么功能的? 我这种方法是通过ADO.net读取出DataSet 后输出的SqlConnection conn=DB.createCon();; SqlDataAdapter da=new SqlDataAdapter("select * from product",conn); DataSet ds=new DataSet(); da.Fill(ds,"table1"); DataTable dt=ds.Tables["table1"]; StringWriter sw=new StringWriter(); //EXCEL显示的列头名sw.WriteLine("自动编号\t姓名\tSid"); //读取数据foreach(DataRow dr in dt.Rows) { sw.WriteLine(dr["pID"]+"\t"+dr["pName"]+"\t"+dr["pSexID"]); } sw.Close(); Response.AddHeader("Content-Disposition", "attachment; filename=test.xls"); Response.ContentType = "application/ms-excel"; Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312"); //输出Response.Write(sw); Response.End(); public void Createxcal(DataSet ds)//服务器端启动 { Excel.Application excel = new Excel.Application(); int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true); excel.Visible = true; DataTable table = ds.Tables[0]; foreach (DataColumn col in table.Columns) { colIndex++; excel.Cells[1, colIndex] = col.ColumnName; } foreach (DataRow row in table.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in table.Columns) { colIndex++; excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } } public void CreateBoweser(DataSet ds)//客户端启动 { HttpContext curContext = HttpContext.Current; StringWriter strwriter = null; HtmlTextWriter htmlwriter = null; DataGrid dg = new DataGrid(); if (ds != null) { curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF8; curContext.Response.Charset = ""; strwriter = new StringWriter(); htmlwriter = new HtmlTextWriter(strwriter); dg.DataSource = ds; dg.AllowPaging = false; dg.DataBind(); dg.RenderControl(htmlwriter); curContext.Response.Write(strwriter.ToString()); curContext.Response.End(); } } public void Createxcal(DataSet ds)//winform { Excel.Application excel = new Excel.Application(); int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true); excel.Visible = true; DataTable table = ds.Tables[0]; foreach (DataColumn col in table.Columns) { colIndex++; excel.Cells[1, colIndex] = col.ColumnName; } foreach (DataRow row in table.Rows) { rowIndex++; colIndex = 0; foreach (DataColumn col in table.Columns) { colIndex++; excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); } } } public void CreateBoweser(DataSet ds)//web { HttpContext curContext = HttpContext.Current; StringWriter strwriter = null; HtmlTextWriter htmlwriter = null; DataGrid dg = new DataGrid(); if (ds != null) { curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = System.Text.Encoding.UTF8; curContext.Response.Charset = ""; strwriter = new StringWriter(); htmlwriter = new HtmlTextWriter(strwriter); dg.DataSource = ds; dg.AllowPaging = false; dg.DataBind(); dg.RenderControl(htmlwriter); curContext.Response.Write(strwriter.ToString()); curContext.Response.End(); } }需要添加 类库 添加com+using Microsoft.Office.Interop.Excel; wlw175733747() 的web方法可以可是我要怎么控制导出到的excel的格式呢?让导出的东西按照我要的格式显示 SQL导出到EXCEL http://www.onlinedown.net/soft/44040.htm 方法一,用ADO.NET连接EXCEL文件写入,缺点是速度很慢,并且要在服务器生成一个临时文件。优点是格式比较好,容易控制。方法二,用response输出datatable,直接输出到客户端,比较快。但有时格式会乱。 可以直接输出字符串,列之间用\t分隔,换行用\r\n如姓名\t性别\r\nA\t\男\r\n显示为:姓名|性别A|男 如何实现在界面点击添加按钮可在DataGridView最后一行中可以添加数据,点击保存按钮,可以保存到数据库中!! C# winform 打包问题(打包高手请进) winform 中treeview的问题 C#net 动态添加DSN 做一个mp3分割软件 zglxh3(noproblem):接分! 急,在线等 GDI+ 自定义基础类库问题 金油条网页正文提取器1.0全新升级 这里有广州深圳的朋友吗,我马上要找工作了,请帮帮忙啊 C#SQL查询 C#中保存文件
{
Response.Clear();
Response.Buffer = true;
//Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=Report.xls");
//Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.divResult.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
} public override void VerifyRenderingInServerForm(Control control)
{ }
{ }
这一段代码是执行什么功能的?
SqlDataAdapter da=new SqlDataAdapter("select * from product",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
DataTable dt=ds.Tables["table1"];
StringWriter sw=new StringWriter();
//EXCEL显示的列头名
sw.WriteLine("自动编号\t姓名\tSid");
//读取数据
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine(dr["pID"]+"\t"+dr["pName"]+"\t"+dr["pSexID"]);
}
sw.Close();
Response.AddHeader("Content-Disposition", "attachment; filename=test.xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
//输出
Response.Write(sw);
Response.End();
{
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
excel.Visible = true;
DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
public void CreateBoweser(DataSet ds)//客户端启动
{
HttpContext curContext = HttpContext.Current;
StringWriter strwriter = null;
HtmlTextWriter htmlwriter = null;
DataGrid dg = new DataGrid();
if (ds != null)
{
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
strwriter = new StringWriter();
htmlwriter = new HtmlTextWriter(strwriter); dg.DataSource = ds;
dg.AllowPaging = false;
dg.DataBind();
dg.RenderControl(htmlwriter);
curContext.Response.Write(strwriter.ToString());
curContext.Response.End();
}
}
{
Excel.Application excel = new Excel.Application();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
excel.Visible = true;
DataTable table = ds.Tables[0];
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
}
public void CreateBoweser(DataSet ds)//web
{
HttpContext curContext = HttpContext.Current;
StringWriter strwriter = null;
HtmlTextWriter htmlwriter = null;
DataGrid dg = new DataGrid();
if (ds != null)
{
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
curContext.Response.Charset = "";
strwriter = new StringWriter();
htmlwriter = new HtmlTextWriter(strwriter); dg.DataSource = ds;
dg.AllowPaging = false;
dg.DataBind();
dg.RenderControl(htmlwriter);
curContext.Response.Write(strwriter.ToString());
curContext.Response.End();
}
}
需要添加 类库
using Microsoft.Office.Interop.Excel;
可是我要怎么控制导出到的excel的格式呢?让导出的东西按照我要的格式显示
优点是格式比较好,容易控制。
方法二,用response输出datatable,直接输出到客户端,比较快。但有时格式会乱。
如
姓名\t性别\r\n
A\t\男\r\n
显示为:
姓名|性别
A|男