已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
C#导出Excel速度慢的解决方法方法一:通过二维数组把数据准备好,一次性写入Excel:private Excel.Application excelApp= new Excel.ApplicationClass();private Excel.Workbook excelBook = excelApp.Workbooks.Add(Type.Missing);private Excel.Worksheet excelSheet = (Excel.Worksheet)excelBook.ActiveSheet; excelApp.Visible = true;public void DataTableToExcel(DataTable dt) int rowCount = dt.Rows.Count;int colCount = dt.Columns.Count;object, dataArray = new objectrowCount,colCount;for (int i = 0; i rowCount; i+)for (int j=0;j colCount;j+)dataArrayi, j = dt.Rowsij;excelSheet.get_Range(A1, excelSheet.CellsrowCount, colCount).Value2 = dataArray;由于二维数组是在内存中实现,所以速度很快,由于是一次性写入到Excel中,使我们几乎感觉不到延迟,在Excel打开后数据马上就出来了,感觉很爽。方法二:private void SaveAs() /另存新档按钮,导出成ExcelDataTable dt = dc.getTable(select * from table);if (dt.Rows.Count = 0) MessageBox.Show(没有要输出为excel的数据!); return;SaveFileDialog saveFileDialog = new SaveFileDialog();saveFileDialog.Filter = Execl files (*.xls)|*.xls;saveFileDialog.FilterIndex = 0;saveFileDialog.RestoreDirectory = true;saveFileDialog.CreatePrompt = true;saveFileDialog.Title = 生成Excel;saveFileDialog.ShowDialog(); Stream myStream;try myStream = saveFileDialog.OpenFile(); /StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(gb2312); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0); try string str = 字段t字段t字段t;/表头 sw.WriteLine(str); for (int i = 0; i dt.Rows.Count; i+) string tempStr = string.Empty;tempStr += dt.Rowsia.ToString() + t;tempStr += dt.Rowsib.ToString() + t;tempStr += dt.Rowsic.ToString() + t;sw.WriteLine(tempStr); /MessageBox.Show(导出成功!); catch (Exception e) sw.Close(); MessageBox.Show(e.ToString(); finally sw.Close(); myStream.Close(); sw.Close(); myStream.Close();catch (Exception e) MessageBox.Show(e.Message); return;方法三:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Drawing;using System.IO;using System.Text;/ / Author:匆匆 Blog:/huangjianhuakarl/ 将Gridview中的数据导出Excel表格/ public partial class GridviewExcel : System.Web.UI.Page protected void Page_Load(object sender, EventArgs e) if (!IsPostBack) bind(); / / 数据绑定 / public void bind() string sqlStr = select * from Employee; DataSet myds = Common.dataSet(sqlStr); GridView1.DataSource = myds; GridView1.DataKeyNames = new string ID ; GridView1.DataBind(); / / 在GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。 / / / protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) foreach (TableCell tc in e.Row.Cells) tc.Attributesstyle = border-color:Black; if (e.Row.RowIndex != -1) int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1; e.Row.Cells0.Text = id.ToString(); / / 导出Excel / / / protected void Button1_Click(object sender, EventArgs e) Export(application/ms-excel, Employee information.xls); / / 定义导出Excel的函数 / / / private void Export(string FileType, string FileName) Response.Charset = GB2312; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AppendHeader(Content-Disposition, attachment;filename= + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString(); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString(); Response.End(); / / 此方法必重写,否则会出错 / / public override void VerifyRenderingInServerF
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年信息安全管理体系评估与改进测试题
- 2025-2026学年大班垃圾分类说课稿
- 2025-2026学年一年级交通安全班会说课稿
- 2025-2026学年动物的尾巴说课稿幼儿园
- 2025-2026学年古典园林说课稿
- 2026年江西省瑞昌市高二生物下册期末考试模拟检测卷附答案(达标题)
- 2025年吉林省公主岭市高考历史真题附答案【黄金题型】
- 2025-2026学年初中地理西欧说课稿
- 2025-2026学年唱国歌说课稿
- 2025-2026学年传统游戏真好玩说课稿
- T/CMSGS 001-2025低空无人驾驶航空器起降点气象观测设施建设和维护要求
- 2026年书记员招聘考试公共基础知识专项试题附答案
- 道路维修验收标准方案
- 2026年小学英语学科专业知识(含新课标核心素养)测试卷含答案(三套)
- 福建省泉州市永春第一中学2025-2026学年高二上学期开学考试数学试卷
- 日本介绍课件
- 感染性伤口的处理原则
- 供应室岗前培训
- 行政应诉实务培训课件
- 2025压力容器设计质量控制程序文件
- 【教师版】表格式审题立意小纸条
评论
0/150
提交评论