




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年保康县统一公开招聘事业单位工作人员笔试有关事项模拟试卷完整答案详解
- 2025河南信阳市潢川县民政局招聘公益性岗位1人考前自测高频考点模拟试题附答案详解(完整版)
- 2025海南陵水黎族自治县中医院(陵水黎族自治县中医院医共体总院)考核招聘(第二批)员额人员模拟试卷含答案详解
- 2025贵州金沙酱酒酒业投资集团有限公司招聘经理层高级管理人员(财务总监)1人考前自测高频考点模拟试题完整参考答案详解
- 2025年郑州新密市国有资产经营有限公司下属文旅板块子公司招聘69人模拟试卷附答案详解(典型题)
- 2025北京中国热带农业科学院香料饮料研究所第一批工作人员招聘(第2号)考前自测高频考点模拟试题及答案详解(各地真题)
- 2025福建亿力集团有限公司所属单位招聘98人考前自测高频考点模拟试题附答案详解
- 2025广东中山长虹电器有限公司散件工艺工程师等岗位考前自测高频考点模拟试题附答案详解(典型题)
- 2025年合肥师范学院高层次人才招聘63人模拟试卷参考答案详解
- 2025年5月广东深圳市光明区应急管理局选聘一般特聘专干1人模拟试卷附答案详解(突破训练)
- 输电线路水泥杆加固防腐施工方案
- 新版医疗器械管理制度零售单体药店
- 小学教师专业发展 教学大纲
- 学校装饰装修工程施工方案
- 烟草证 申请书
- 屋面光伏工程施工组织设计
- 山体公园施工方案
- DL-T 5876-2024 水工沥青混凝土应用酸性骨料技术规范
- 胆囊癌完整版本
- 【MOOC】数据库原理及应用-电子科技大学 中国大学慕课MOOC答案
- 节约集约建设用地标准 DG-TJ08-2422-2023
评论
0/150
提交评论