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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格

發(fā)布時間:2023/11/27 生活经验 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最近因為需要學習了一下使用C#操作Excel表格,現(xiàn)在把我使用C#如何定制表格的過程提供給需要的兄弟:
/*從數(shù)據(jù)庫提取數(shù)據(jù)*/
string strconn="packet size=4096;user id=sa;data source=localhost;persist security info=True;initial catalog=Database;password=sa";
SqlConnection sqlconn=new SqlConnection(strconn);
sqlconn.Open();
SqlDataAdapter sqldataAdapter=new SqlDataAdapter("Select * from [數(shù)據(jù)表]",sqlconn);
DataSet myds=new DataSet();
sqldataAdapter.Fill(myds);
/*在Execl中建立“成果表”的格式表格*/
Excel.ApplicationClass excel=new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Cells[1,1] = "單元名稱" ;
excel.Cells[1,2]="指標及其描述、特征值和權(quán)重";
excel.Cells[1,23]="屬性";
excel.Cells[2,2]="D";
excel.Cells[2,5]="R";
excel.Cells[2,8]="A";
excel.Cells[2,11]="S";
excel.Cells[2,14]="T";
excel.Cells[2,17]="I";
excel.Cells[2,20]="C";
excel.Cells[2,23]="結(jié)果";
excel.Cells[2,24]="等級";
excel.Cells[3,2]="描述";
excel.Cells[3,3]="特征值";
excel.Cells[3,4]="權(quán)重";
excel.Cells[3,5]="描述";
excel.Cells[3,6]="特征值";
excel.Cells[3,7]="權(quán)重";
excel.Cells[3,8]="描述";
excel.Cells[3,9]="特征值";
excel.Cells[3,10]="權(quán)重";
excel.Cells[3,11]="描述";
excel.Cells[3,12]="特征值";
excel.Cells[3,13]="權(quán)重";
excel.Cells[3,14]="描述";
excel.Cells[3,15]="特征值";
excel.Cells[3,16]="權(quán)重";
excel.Cells[3,17]="描述";
excel.Cells[3,18]="特征值";
excel.Cells[3,19]="權(quán)重";
excel.Cells[3,20]="描述";
excel.Cells[3,21]="特征值";
excel.Cells[3,22]="權(quán)重";

excel.get_Range(excel.Cells[ 1 , 1 ],excel.Cells[ 3 ,1 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 1 , 2 ],excel.Cells[ 1 ,22 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 1 , 23 ],excel.Cells[ 1 ,24 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 2 ],excel.Cells[ 2 ,4 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 5 ],excel.Cells[ 2 ,7 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 8],excel.Cells[ 2 ,10 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 ,11 ],excel.Cells[ 2 ,13 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 14 ],excel.Cells[ 2 ,16 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 17 ],excel.Cells[ 2 ,19 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 20 ],excel.Cells[ 2 ,22 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 23 ],excel.Cells[ 3 ,23 ]).MergeCells=true;
excel.get_Range(excel.Cells[ 2 , 24 ],excel.Cells[ 3 ,24 ]).MergeCells=true;

/*往Excel格式表格中寫入數(shù)據(jù)*/
int waterdataRows=myds.Tables[0].Rows.Count;
int waterdataCols=myds.Tables[0].Columns.Count;
for(int i=1;i<=waterdataRows;i++)
{
excel.Cells[i+3,1]=myds.Tables[0].Rows[i-1].ItemArray.GetValue(0);
for(int j=1;j<=7;j++)
{
excel.Cells[i+3,3*j]=myds.Tables[0].Rows[i-1].ItemArray.GetValue(j);
excel.Cells[i+3,3*j+1]=WW[j-1];
}
excel.Cells[i+3,23]=ds.Tables[0].Rows[i-1].ItemArray.GetValue(1);
excel.Cells[i+3,24]=ds.Tables[0].Rows[i-1].ItemArray.GetValue(3);
}
excel.Visible=false;
excel.DisplayAlerts=false;
//Excel.WorksheetClass worksheet=(Excel.WorksheetClass)excel.ActiveSheet;
excel.Save();
excel.Application.Workbooks.Close();
excel.Application.Quit();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
KillProcess("EXCEL.EXE");
GC.Collect();?
?

總結(jié)

以上是生活随笔為你收集整理的以实例说明如何使用C#从数据库中提取数据,按要求自动生成定制的Excel表格的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。