C #. Net Process execl

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. reflection;
Using system. Windows. forms;

Namespace pal_mes.brlibrary
{
Class cdealexcel
{
Public void exporttoexcel (datatable DT)
{
Excel. Application xlapp = new excel. Application ();
Excel. Workbook xlbook = xlapp. application. workbooks. Add (true );
Excel. worksheet xlsheet = (Excel. worksheet) xlbook. activesheet;
Xlsheet. Name = "result ";
Excel. Range RA;
Xlapp. Visible = true;
Xlapp. usercontrol = false;
Xlapp. displayalerts = false;

Int irow = DT. Rows. count;
Int icol = DT. Columns. count;

String [,] x = new string [irow, 1];
Int I = 1;
Foreach (datacolumn DC in DT. columns)
{
Xlsheet. cells [1, I] = DT. Columns [I-1]. Caption. tostring ();

Int J = 0;
Foreach (datarow DR in DT. Rows)
{
X [J, 0] = Dr [I-1]. tostring ();
J = J + 1;
}
Ra = xlsheet. get_range (xlsheet. cells [2, I], xlsheet. cells [irow + 1, I]);
RA. value2 = X;
RA. entirecolumn. autofit ();
I = I + 1;
}
Ra = xlsheet. get_range (xlsheet. cells [1, 1], xlsheet. cells [DT. Rows. Count + 1, DT. Columns. Count]);
RA. Borders. linestyle = excel. xllinestyle. xlcontinuous;
RA. Font. Name = "Arial ";
RA. Font. size = 10;
Ra = xlsheet. get_range (xlsheet. cells [1, 1], xlsheet. cells [1, DT. Columns. Count]);
RA. Interior. colorindex = 45;
RA. horizontalalignment = excel. xlhalign. xlhaligncenter;

Xlsheet = NULL;
Xlbook = NULL;
Xlapp = NULL;
GC. Collect ();
}

Public datatable getsheetfromexcel (string filename)
{
Missing Miss = missing. value;
Datatable dtsheet = new datatable ();
Excel. Application xlapp = new excel. Application ();
Excel. workbook xlbook = xlapp. workbooks. open (filename, miss, miss );
Dtsheet. Columns. Add ("sheetname ");
For (INT I = 1; I <= xlbook. Sheets. Count; I ++)
{
Dtsheet. Rows. Add (Excel. worksheet) xlbook. worksheets [I]). Name. tostring ());
}

Xlapp. Quit ();
Xlbook = NULL;
Xlapp = NULL;
Return dtsheet;

}

}
}

 

Call Statement: cexcel. exporttoexcel (DT); cexcel class name.

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.