日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【原】winform高效导出Excel带格式设置

發布時間:2023/12/10 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【原】winform高效导出Excel带格式设置 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考網上的方法修改,1000條記錄導出只要3S(1GRDM,C2.8CPU).
1.項目添加excel對象類庫的引用,Microsoft Excel 11.0 object library(不同版本的Excel,類庫不同,這是2003的)?
2.代碼

??1//-***************獲取要寫入excel的數據源***************
??2????????????Dao?model=new?Dao();
??3????????????DataTable?dt=model.GetFileNameList(0,intPageSize,Convert.ToDateTime(dtPFrom.Value),?Convert.ToDateTime(dtPTo.Value));//取得dataGrid綁定的DataSet
??4????????????if(dt==null)?return;
??5????????????DataGridTableStyle?ts?=?dataGrid1.TableStyles[0];
??6
??7
??8????????????//-***************獲取excel對象***************
??9????????????string?saveFileName="";
?10????????????bool?fileSaved=false;
?11????????????SaveFileDialog?saveDialog=new?SaveFileDialog();
?12????????????saveDialog.DefaultExt?="xls";
?13????????????saveDialog.Filter="Excel文件|*.xls";
?14????????????saveDialog.FileName?="導入記錄查詢結果?"+DateTime.Today.ToString("yyyy-MM-dd");
?15????????????saveDialog.ShowDialog();
?16????????????saveFileName=saveDialog.FileName;
?17????????????if(saveFileName.IndexOf(":")<0)?return;?//被點了取消
?18????????????Excel.Application?xlApp=new?Excel.Application();
?19????????????if(xlApp==null)
?20????????????{
?21????????????????MessageBox.Show("無法啟動Excel,可能您的機子未安裝Excel");
?22????????????????return;
?23????????????}

?24????????????Excel.Workbook?workbook?=?xlApp.Workbooks.Add(true);
?25????????????Excel.Worksheet?worksheet?=?(Excel.Worksheet)workbook.Worksheets[1];???
?26????????????Excel.Range?range;
?27
?28???????
?29????????????string?oldCaption=dataGrid1.CaptionText;
?30????????????//?列索引,行索引,總列數,總行數???????????????????
?31????????????int?colIndex?=?0;
?32????????????int?RowIndex?=?0;
?33????????????int?colCount?=?ts.GridColumnStyles.Count;
?34????????????int?RowCount=dt.Rows.Count;
?35???????????
?36
?37????????????//?*****************獲取數據*********************
?38????????????dataGrid1.CaptionVisible?=?true;
?39????????????dataGrid1.CaptionText?=?"正在導出數據";
?40????????????//?創建緩存數據
?41????????????object[,]?objData?=?new?object[RowCount?+?1,?colCount];
?42????????????//?獲取列標題
?43????????????foreach(DataGridColumnStyle?cs?in?ts.GridColumnStyles)
?44????????????{
?45????????????????objData[RowIndex,colIndex++]?=?cs.HeaderText;???
?46????????????}

?47???????
?48????????????//?獲取具體數據
?49????????????for(RowIndex?=1;RowIndex<?RowCount;RowIndex++)
?50????????????{
?51????????????????for(colIndex=0;colIndex?<?colCount;colIndex++)
?52????????????????{???????????????
?53????????????????????objData[RowIndex,colIndex]?=dt.Rows[RowIndex-1][colIndex+1];
?54????????????????}

?55???????????????
?56????????????}
???
?57
?58???????????//*********************?寫入Excel*******************
?59???????????????
?60????????????range?=?worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[RowCount,colCount]);???????????
?61????????????range.Value2=?objData;????????????
?62????????????Application.DoEvents();???
?63???????????
?64????????????//*******************設置輸出格式******************************
?65?????????????
?66????????????//設置頂部説明???
?67????????????range?=?worksheet.get_Range(xlApp.Cells[1,1],xlApp.Cells[1,colCount]);
?68????????????range.MergeCells?=?true;
?69????????????range.RowHeight=38;
?70????????????range.Font.Bold=true;
?71????????????range.Font.Size=14;
?72????????????range.Font.ColorIndex=10;//字體顏色
?73????????????xlApp.ActiveCell.FormulaR1C1?=?"導入記錄查詢結果";
?74
?75????????????//特殊數字格式
?76????????????range?=?worksheet.get_Range(xlApp.Cells[2,colCount],xlApp.Cells[RowCount,colCount]);
?77????????????range.NumberFormat="yyyy-MM-dd?hh:mm:ss";
?78
?79????????????xlApp.Cells.HorizontalAlignment=Excel.Constants.xlCenter;???
?80????????????range?=?worksheet.get_Range(xlApp.Cells[2,1],xlApp.Cells[2,colCount]);
?81????????????range.Interior.ColorIndex?=?10;//背景色
?82????????????range.Font.Bold?=?true;
?83????????????range.RowHeight=20;
?84????????????((Excel.Range)worksheet.Cells[2,1]).ColumnWidth=25;
?85????????????((Excel.Range)worksheet.Cells[2,2]).ColumnWidth=13;
?86????????????((Excel.Range)worksheet.Cells[2,3]).ColumnWidth=18;
?87????????????((Excel.Range)worksheet.Cells[2,4]).ColumnWidth=15;
?88????????????((Excel.Range)worksheet.Cells[2,5]).ColumnWidth=22;
?89
?90????????????//***************************保存**********************
?91????????????dataGrid1.CaptionVisible?=?false;
?92????????????dataGrid1.CaptionText?=?oldCaption;?
?93????????????if(saveFileName!="")
?94????????????{
?95????????????????try
?96????????????????{
?97????????????????????workbook.Saved?=true;
?98????????????????????workbook.SaveCopyAs(saveFileName);
?99????????????????????fileSaved=true;
100????????????????}

101????????????????catch(Exception?ex)
102????????????????{
103????????????????????fileSaved=false;
104????????????????????MessageBox.Show("導出文件時出錯,文件可能正被打開!\n"+ex.Message);
105????????????????}

106????????????}

107????????????else
108????????????{
109????????????????fileSaved=false;
110????????????}

111????????????xlApp.Quit();
112????????????GC.Collect();//強行銷毀???
113????????????TimeSpan?dateEnd=new?TimeSpan(DateTime.Now.Ticks);???????????
114????????????TimeSpan?tspan=dateBegin.Subtract(dateEnd).Duration();
115????????????MessageBox.Show(tspan.ToString());
116????????????if(fileSaved?&&?File.Exists(saveFileName))
117????????????????System.Diagnostics.Process.Start(saveFileName);

轉載于:https://www.cnblogs.com/Areas/archive/2012/08/07/2627039.html

總結

以上是生活随笔為你收集整理的【原】winform高效导出Excel带格式设置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。