(ASP.NET)C#导出EXCEL
4个回答
展开全部
是一样的啊!
具体用法如下:有的地方你自己改改啊,因为是我自己的项目里面的。
先导入一下包:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
if (System.IO.File.Exists(this.MapPath("\\userField\\empinfo.xls")))
{
System.IO.File.Delete(this.MapPath("\\userField\\empinfo.xls"));
}
wk.SaveAs(this.MapPath("\\userField\\empinfo.xls"), ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();
this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../userField/empinfo.xls');", true);
具体用法如下:有的地方你自己改改啊,因为是我自己的项目里面的。
先导入一下包:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
object ms = Type.Missing;
Excel.Workbook wk = excel.Workbooks.Add(ms);
Excel.Worksheet ws = wk.Worksheets[1] as Excel.Worksheet;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
}
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
}
}
if (System.IO.File.Exists(this.MapPath("\\userField\\empinfo.xls")))
{
System.IO.File.Delete(this.MapPath("\\userField\\empinfo.xls"));
}
wk.SaveAs(this.MapPath("\\userField\\empinfo.xls"), ms, ms, ms, ms, ms, Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
excel.Quit();
this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../userField/empinfo.xls');", true);
展开全部
首先,在导出按钮里写上Response.Redirect("ExportHandler.ashx"); ExportHandler.ashx是一个一般处理程序文件.
在一般处理程序这样写:
public class ExportHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState //一定要继承这个接口
{
private const string header =
@"<?xml version='1.0'?>
<?mso-application progid='Excel.Sheet'?>
<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:html='http://www.w3.org/TR/REC-html40'>
<Worksheet ss:Name='TEST'>
<Table>
<Row>
<Cell><Data ss:Type='String'>ID</Data></Cell>
<Cell><Data ss:Type='String'>Name</Data></Cell>
<Cell><Data ss:Type='String'>Height</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
";//这上面都是死的东西,除了id,name,height是你要导入的列,你想导出多少列就定义多少
private const string defaultUri = "urn:schemas-microsoft-com:office:spreadsheet";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/vnd.ms-excel";
List<Student> students = context.Session["Data"] as List<Student>;//这个Session["Data"]里的data是查到数据后保存在session里的,这里取出来,List<Student>你应该很清楚吧,你查出来的数据是什么类型的就写什么
XmlDocument doc;
doc = new XmlDocument();
doc.LoadXml(header);
XmlElement eTable = doc.DocumentElement.FirstChild as XmlElement;
foreach (Student student in students)
{
XmlElement row = CreateRow(doc, student);
if (null != row)
{
eTable.AppendChild(row);
}
}//这里遍历的Student也一样
context.Response.Write(doc.OuterXml);
}
private XmlElement CreateRow(XmlDocument doc, Student s)
{
XmlElement row = doc.CreateElement("Row", defaultUri);
XmlElement eID = CreateCell(doc, s.Code, "ID", "String");
XmlElement eName = CreateCell(doc, s.Name, "Code", "String");
XmlElement eHeight = CreateCell(doc, s.Height, "Height", "Number");
//这里都是你要导出的属性名
row.AppendChild(eID);
row.AppendChild(eName);
row.AppendChild(eHeight);
return row;
}
private XmlElement CreateCell(XmlDocument doc, object value, string name, string type)
{
XmlElement e = doc.CreateElement("Cell", defaultUri);
XmlElement eData = doc.CreateElement("Data", defaultUri);
eData.SetAttribute("Type", defaultUri, type);
eData.InnerText = (null == value ? string.Empty : value.ToString());
e.AppendChild(eData);
return e;
}
public bool IsReusable {
get {
return false;
}
}
}
实在不明白的话,留下邮箱,我给你发个简单的列子!
在一般处理程序这样写:
public class ExportHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState //一定要继承这个接口
{
private const string header =
@"<?xml version='1.0'?>
<?mso-application progid='Excel.Sheet'?>
<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:x='urn:schemas-microsoft-com:office:excel'
xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'
xmlns:html='http://www.w3.org/TR/REC-html40'>
<Worksheet ss:Name='TEST'>
<Table>
<Row>
<Cell><Data ss:Type='String'>ID</Data></Cell>
<Cell><Data ss:Type='String'>Name</Data></Cell>
<Cell><Data ss:Type='String'>Height</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
";//这上面都是死的东西,除了id,name,height是你要导入的列,你想导出多少列就定义多少
private const string defaultUri = "urn:schemas-microsoft-com:office:spreadsheet";
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/vnd.ms-excel";
List<Student> students = context.Session["Data"] as List<Student>;//这个Session["Data"]里的data是查到数据后保存在session里的,这里取出来,List<Student>你应该很清楚吧,你查出来的数据是什么类型的就写什么
XmlDocument doc;
doc = new XmlDocument();
doc.LoadXml(header);
XmlElement eTable = doc.DocumentElement.FirstChild as XmlElement;
foreach (Student student in students)
{
XmlElement row = CreateRow(doc, student);
if (null != row)
{
eTable.AppendChild(row);
}
}//这里遍历的Student也一样
context.Response.Write(doc.OuterXml);
}
private XmlElement CreateRow(XmlDocument doc, Student s)
{
XmlElement row = doc.CreateElement("Row", defaultUri);
XmlElement eID = CreateCell(doc, s.Code, "ID", "String");
XmlElement eName = CreateCell(doc, s.Name, "Code", "String");
XmlElement eHeight = CreateCell(doc, s.Height, "Height", "Number");
//这里都是你要导出的属性名
row.AppendChild(eID);
row.AppendChild(eName);
row.AppendChild(eHeight);
return row;
}
private XmlElement CreateCell(XmlDocument doc, object value, string name, string type)
{
XmlElement e = doc.CreateElement("Cell", defaultUri);
XmlElement eData = doc.CreateElement("Data", defaultUri);
eData.SetAttribute("Type", defaultUri, type);
eData.InnerText = (null == value ? string.Empty : value.ToString());
e.AppendChild(eData);
return e;
}
public bool IsReusable {
get {
return false;
}
}
}
实在不明白的话,留下邮箱,我给你发个简单的列子!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单方法:
首先把数据绑定到datagrid(可以隐藏datagrid)
//override掉这个方法
public override void VerifyRenderingInServerForm(Control control)
{
//注释掉下面的代码,否则在asp.net2.0下会报错(注:GridView是asp.net 2.0下的控件,1.1下一些控件也可以导出成Excel或者Word)
//base.VerifyRenderingInServerForm(control);
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=test.xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//设置导出文件的格式
Response.ContentType = "application/ms-excel";
//关闭ViewState
EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("EN-US", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
datagrid1.RenderControl(textWriter);
//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}
首先把数据绑定到datagrid(可以隐藏datagrid)
//override掉这个方法
public override void VerifyRenderingInServerForm(Control control)
{
//注释掉下面的代码,否则在asp.net2.0下会报错(注:GridView是asp.net 2.0下的控件,1.1下一些控件也可以导出成Excel或者Word)
//base.VerifyRenderingInServerForm(control);
}
protected void btnExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "UTF-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=test.xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
//设置导出文件的格式
Response.ContentType = "application/ms-excel";
//关闭ViewState
EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("EN-US", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
datagrid1.RenderControl(textWriter);
//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
那肯定是你的用法错了,采用XML格式的形式用流来写文件是很块的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |