Using examples to illustrate how to use C # to extract data from the database and generate custom Excel tables as required?

Source: Internet
Author: User
/* Extract data from the database */
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 = new sqldataadapter ("select * from [data table]", sqlconn );
Dataset myds = new dataset ();
Sqldataadapter. Fill (myds );
/* Create a "result table" format table in execl */
Excel. applicationclass Excel = new excel. applicationclass ();
Excel. application. workbooks. Add (true );
Excel. cells [1, 1] = "unit name ";
Excel. cells [] = "indicators and their descriptions, feature values and weights ";
Excel. cells [] = "properties ";
Excel. cells [2, 2] = "D ";
Excel. cells [2, 5] = "R ";
Excel. cells [2, 8] = "";
Excel. cells [2, 11] = "S ";
Excel. cells [2, 14] = "T ";
Excel. cells [2, 17] = "I ";
Excel. cells [2, 20] = "C ";
Excel. cells [2, 23] = "result ";
Excel. cells [2, 24] = "Grade ";
Excel. cells [3, 2] = "Description ";
Excel. cells [3, 3] = "feature value ";
Excel. cells [3, 4] = "weight ";
Excel. cells [3, 5] = "Description ";
Excel. cells [3, 6] = "feature value ";
Excel. cells [3, 7] = "weight ";
Excel. cells [3, 8] = "Description ";
Excel. cells [3, 9] = "feature value ";
Excel. cells [3, 10] = "weight ";
Excel. cells [3, 11] = "Description ";
Excel. cells [3, 12] = "feature value ";
Excel. cells [3, 13] = "weight ";
Excel. cells [3, 14] = "Description ";
Excel. cells [3, 15] = "feature value ";
Excel. cells [3, 16] = "weight ";
Excel. cells [3, 17] = "Description ";
Excel. cells [3, 18] = "feature value ";
Excel. cells [3, 19] = "weight ";
Excel. cells [3, 20] = "Description ";
Excel. cells [3, 21] = "feature value ";
Excel. cells [3, 22] = "weight ";

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;

/* Write data to an Excel format table */
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 +] = Ds. Tables [0]. Rows [I-1]. itemarray. getvalue (1 );
Excel. cells [I +] = 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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.