【原】winform高效导出Excel带格式设置
生活随笔
收集整理的這篇文章主要介紹了
【原】winform高效导出Excel带格式设置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
參考網(wǎng)上的方法修改,1000條記錄導(dǎo)出只要3S(1GRDM,C2.8CPU).
1.項(xiàng)目添加excel對(duì)象類庫(kù)的引用,Microsoft Excel 11.0 object library(不同版本的Excel,類庫(kù)不同,這是2003的)?
2.代碼
??1//-***************獲取要寫入excel的數(shù)據(jù)源***************
??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對(duì)象***************
??9????????????string?saveFileName="";
?10????????????bool?fileSaved=false;
?11????????????SaveFileDialog?saveDialog=new?SaveFileDialog();
?12????????????saveDialog.DefaultExt?="xls";
?13????????????saveDialog.Filter="Excel文件|*.xls";
?14????????????saveDialog.FileName?="導(dǎo)入記錄查詢結(jié)果?"+DateTime.Today.ToString("yyyy-MM-dd");
?15????????????saveDialog.ShowDialog();
?16????????????saveFileName=saveDialog.FileName;
?17????????????if(saveFileName.IndexOf(":")<0)?return;?//被點(diǎn)了取消
?18????????????Excel.Application?xlApp=new?Excel.Application();
?19????????????if(xlApp==null)
?20????????????{
?21????????????????MessageBox.Show("無(wú)法啟動(dòng)Excel,可能您的機(jī)子未安裝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????????????//?列索引,行索引,總列數(shù),總行數(shù)???????????????????
?31????????????int?colIndex?=?0;
?32????????????int?RowIndex?=?0;
?33????????????int?colCount?=?ts.GridColumnStyles.Count;
?34????????????int?RowCount=dt.Rows.Count;
?35???????????
?36
?37????????????//?*****************獲取數(shù)據(jù)*********************
?38????????????dataGrid1.CaptionVisible?=?true;
?39????????????dataGrid1.CaptionText?=?"正在導(dǎo)出數(shù)據(jù)";
?40????????????//?創(chuàng)建緩存數(shù)據(jù)
?41????????????object[,]?objData?=?new?object[RowCount?+?1,?colCount];
?42????????????//?獲取列標(biāo)題
?43????????????foreach(DataGridColumnStyle?cs?in?ts.GridColumnStyles)
?44????????????{
?45????????????????objData[RowIndex,colIndex++]?=?cs.HeaderText;???
?46????????????}
?47???????
?48????????????//?獲取具體數(shù)據(jù)
?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????????????//*******************設(shè)置輸出格式******************************
?65?????????????
?66????????????//設(shè)置頂部説明???
?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?=?"導(dǎo)入記錄查詢結(jié)果";
?74
?75????????????//特殊數(shù)字格式
?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("導(dǎo)出文件時(shí)出錯(cuò),文件可能正被打開(kāi)!\n"+ex.Message);
105????????????????}
106????????????}
107????????????else
108????????????{
109????????????????fileSaved=false;
110????????????}
111????????????xlApp.Quit();
112????????????GC.Collect();//強(qiáng)行銷毀???
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);
1.項(xiàng)目添加excel對(duì)象類庫(kù)的引用,Microsoft Excel 11.0 object library(不同版本的Excel,類庫(kù)不同,這是2003的)?
2.代碼
??1//-***************獲取要寫入excel的數(shù)據(jù)源***************
??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對(duì)象***************
??9????????????string?saveFileName="";
?10????????????bool?fileSaved=false;
?11????????????SaveFileDialog?saveDialog=new?SaveFileDialog();
?12????????????saveDialog.DefaultExt?="xls";
?13????????????saveDialog.Filter="Excel文件|*.xls";
?14????????????saveDialog.FileName?="導(dǎo)入記錄查詢結(jié)果?"+DateTime.Today.ToString("yyyy-MM-dd");
?15????????????saveDialog.ShowDialog();
?16????????????saveFileName=saveDialog.FileName;
?17????????????if(saveFileName.IndexOf(":")<0)?return;?//被點(diǎn)了取消
?18????????????Excel.Application?xlApp=new?Excel.Application();
?19????????????if(xlApp==null)
?20????????????{
?21????????????????MessageBox.Show("無(wú)法啟動(dòng)Excel,可能您的機(jī)子未安裝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????????????//?列索引,行索引,總列數(shù),總行數(shù)???????????????????
?31????????????int?colIndex?=?0;
?32????????????int?RowIndex?=?0;
?33????????????int?colCount?=?ts.GridColumnStyles.Count;
?34????????????int?RowCount=dt.Rows.Count;
?35???????????
?36
?37????????????//?*****************獲取數(shù)據(jù)*********************
?38????????????dataGrid1.CaptionVisible?=?true;
?39????????????dataGrid1.CaptionText?=?"正在導(dǎo)出數(shù)據(jù)";
?40????????????//?創(chuàng)建緩存數(shù)據(jù)
?41????????????object[,]?objData?=?new?object[RowCount?+?1,?colCount];
?42????????????//?獲取列標(biāo)題
?43????????????foreach(DataGridColumnStyle?cs?in?ts.GridColumnStyles)
?44????????????{
?45????????????????objData[RowIndex,colIndex++]?=?cs.HeaderText;???
?46????????????}
?47???????
?48????????????//?獲取具體數(shù)據(jù)
?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????????????//*******************設(shè)置輸出格式******************************
?65?????????????
?66????????????//設(shè)置頂部説明???
?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?=?"導(dǎo)入記錄查詢結(jié)果";
?74
?75????????????//特殊數(shù)字格式
?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("導(dǎo)出文件時(shí)出錯(cuò),文件可能正被打開(kāi)!\n"+ex.Message);
105????????????????}
106????????????}
107????????????else
108????????????{
109????????????????fileSaved=false;
110????????????}
111????????????xlApp.Quit();
112????????????GC.Collect();//強(qiáng)行銷毀???
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);
轉(zhuǎn)載于:https://www.cnblogs.com/Areas/archive/2012/08/07/2627039.html
總結(jié)
以上是生活随笔為你收集整理的【原】winform高效导出Excel带格式设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【转】删除已经存在的 TFS Works
- 下一篇: Android开发学习笔记-自定义对话框