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