將查詢(xún)數(shù)據(jù)通過(guò)C#導(dǎo)入Excel表的簡(jiǎn)單方法
作者:佚名
.NET封裝了EXCEL相關(guān)的類(lèi),查詢(xún)數(shù)據(jù)是可以通過(guò)C#導(dǎo)入Excel表的。以下是實(shí)現(xiàn)在程序中標(biāo)工具條中點(diǎn)擊“EXCEL輸出”按鈕而觸發(fā)的事件,前提是你的系統(tǒng)中裝有EXCEL。
將查詢(xún)數(shù)據(jù)通過(guò)C#導(dǎo)入Excel表的簡(jiǎn)單方法在很多人看來(lái)是不切實(shí)際的。Excel的數(shù)據(jù)處理能力比不上MySQL等專(zhuān)業(yè)數(shù)據(jù)庫(kù),不過(guò)Excel的廣泛使用性和便捷性,還是很需要關(guān)注一下Excel的。
這是我的代碼,有注釋說(shuō)明:
- private void toolBar1_Butt(object sender, System.Windows.Forms.ToolBarButtEventArgs e)
- {//工具條各個(gè)按紐單擊事件
- if(e.Button==excelOut)
- {
- Excel.Application excelKccx = new Excel.Application();//創(chuàng)建excel對(duì)象
- excelKccx.Workbooks.Add(true);//創(chuàng)建excel工作薄
- DataTable myDataTable=myDataSet.Tables["庫(kù)存信息"];//創(chuàng)建一個(gè)數(shù)據(jù)表,得到DataSet中“庫(kù)存信息”表中的數(shù)據(jù)
- int row=2;
- //把數(shù)據(jù)表的各個(gè)信息輸入,利用C#導(dǎo)入excel表中
- for(int i=0;i {
- excelKccx.Cells[1,i+1]=myDataTable.Columns.ColumnName.ToString();
- }
- for(int i=0;i { for(int j=0;j {
- excelKccx.Cells[row,j+1]=myDataTable.Rows[j].ToString();
- }
- row++;
- }
- excelKccx.Visible=true;//使excel可見(jiàn)*/
- }
- else if(e.Button==reportForm)
- {
- //kcRptForm myReport=new kcRptForm();
- //myReport.MdiParent=this;
- //myReport.Show();
- }
將查詢(xún)數(shù)據(jù)通過(guò)C#導(dǎo)入Excel表的簡(jiǎn)單方法就介紹到這里。
【編輯推薦】
責(zé)任編輯:彭凡
來(lái)源:
51CTO論壇