DevExpress GridControl GridView大批量数据(20万条)导出Excel, 由于03版的Excel每个Sheet页只能承载65536条数据,故自带的ExportToExcelOld及ExportToPdf函数在导出20W条数据时,只能导出前65536条数据,其他数据丢失。 所以自己写导出函数,可以分sheet页来写,用到Excel组件,但是速度比较慢,现用数据流的方式来写,代码如下:
1public static void GridViewToExcel(Stream myStream, DevExpress.XtraGrid.Views.Grid.GridView dataGridView1)
2 { 3 StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); 4 string str = ""; 5 try 6 { 7 //写标题 8 for (int i = 1; i < dataGridView1.Columns.Count; i++) 9 { 10 if (!string.IsNullOrEmpty(dataGridView1.Columns[i].Caption)) 11 { 12 if (i > 1) 13 { 14 str += "\t"; 15 } 16 str += dataGridView1.Columns[i].Caption; 17 } 18 } 19 20 sw.WriteLine(str); 21 //写内容 22 for (int j = 0; j < dataGridView1.RowCount; j++) 23 { 24 string tempStr = ""; 25 for (int k = 1; k < dataGridView1.Columns.Count; k++) 26 { 27 if (!string.IsNullOrEmpty(dataGridView1.Columns[k].Caption)) 28 { 29 if (k > 1) 30 { 31 tempStr += "\t"; 32 } 33 tempStr += dataGridView1.GetRowCellValue(j, dataGridView1.Columns[k].FieldName); 34 } 35 } 36 sw.WriteLine(tempStr); 37 } 38 sw.Close(); 39 myStream.Close(); 40 } 41 catch (Exception ex) 42 { 43 MessageBox.Show(ex.ToString()); 44 } 45 finally 46 { 47 sw.Close(); 48 myStream.Close(); 49 } 50 51 } |
|