求 asp.net mvc2.0框架 导出EXCEL 例子?
解决方案 »
- select top中加变量报错
- 在母板页里如何获取控件的name
- 如何在删除前弹出确认对话框???
- 请问生成验证码时怎么把图片上的文字画成倾斜的啊
- 有codemax的代码?
- 在.net中可有转义字符?
- 高分!请问如何将数据库中的记录导出更新一个存在的excel的一个sheet?最好有代码!急!!!
- 关于论坛发贴的问题
- ASP.NET AJAX 1.0 中如何实现:在页面中点击按钮打开新页面,输入数据,然后返回到初始页面并进行更新,并且只是更新父窗体中的GridView
- 关于web customer control的问题
- C#导出数据到Excel过程中遇到的异常来自 HRESULT:0x800A03EC
- Asp.net思路问题(在线等)
{
Excel.Application m_Excel = null;
Excel._Workbook m_Book = null;
Excel._Worksheet wsOne = null;
Excel.Range range = null; string strOneTitle = "单期数据";
m_Excel = new Excel.Application();//创建一个Excel对象(同时启动EXCEL.EXE进程)
m_Excel.SheetsInNewWorkbook = 1;//工作表的个数
m_Book = (Excel._Workbook)(m_Excel.Workbooks.Add(Missing.Value));//添加新工作簿 //添加“股东数据结构分析-单期数据”
wsOne = (Excel._Worksheet)(m_Book.Worksheets["Sheet1"]);
wsOne.Name = "StructOne";
try
{
if(ViewState["OneDate"].ToString() != "")
{
#region 添加“单期数据”到Excel的指定Sheet中
System.Data.DataTable dtOne = (System.Data.DataTable)ViewState["dtInfo"];
//以下是填写EXCEL中数据
range = wsOne.get_Range(wsOne.Cells[1,1], wsOne.Cells[1,4]);
range.MergeCells = true; //合并单元格
range.Font.Bold = true; //加粗单元格内字符
wsOne.Cells[1,1] = strOneTitle;
wsOne.Cells[2,1] = "类别";
wsOne.Cells[2,2] = "人数";
wsOne.Cells[2,3] = "数量";
wsOne.Cells[2,4] = "比例(%)"; for(int i=0; i<dtOne.Rows.Count; i++)
{
wsOne.Cells[i+3,1] = dtOne.Rows[i]["Type"];
wsOne.Cells[i+3,2] = dtOne.Rows[i]["PerNum"];
wsOne.Cells[i+3,3] = dtOne.Rows[i]["HaveNum"];
wsOne.Cells[i+3,4] = dtOne.Rows[i]["HaveRate"];
}
wsOne.Columns.AutoFit();
#endregion
}
m_Book.SaveAs(strFilePath + strFileType,Excel.XlFileFormat.xlWebArchive, Missing.Value,Missing.Value,Missing.Value,Missing.Value,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
m_Book.Close(false, Missing.Value, Missing.Value);
m_Excel.Workbooks.Close();
m_Book = null;
m_Excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
int generation=System.GC.GetGeneration(m_Excel);
m_Excel = null;
System.GC.Collect(generation);
GC.Collect();
//Kill();
string fileSavePath = strFilePath + ".xls";
string filename = "InfoFrame.xls";
string strContentType = "application/ms-excel";
FileStream fs = new FileStream(fileSavePath ,FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes,0,bytes.Length);
fs.Close();
Response.ContentType = strContentType;
Response.AddHeader("Content-Disposition", "attachment; filename = " + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
}
catch(Exception)
{
}
finally
{
GC.Collect();
}
}
已经解决了
可以看一下我的解决方案,希望可以帮到LZ
http://topic.csdn.net/u/20110831/15/fcd43dd2-44a4-460d-a737-5d11fe53b587.html