C # import table to excel

Source: Internet
Author: User

Problems encountered in the sub-project a while ago .... At that time, no solution was found on the Internet... Send one today...

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. diagnostics;
Using Excel;
Using system. text;

/// <Summary>
/// Tabletoexcel

/// Import table to excel

/// </Summary>
Public class tabletoexcel
{
Public tabletoexcel ()
{
// Todo: add the constructor logic here
}

Public void toexcel (Table T, String title)
{
Excel. applicationclass Ea = new applicationclass ();
Excel. Workbook WB = EA. workbooks. Add (true );


Excel. worksheet Ws = WB. activesheet as Excel. worksheet;
Int Col = T. Rows [(T. Rows. Count-1)]. cells. count;
Excel. Range = ws. get_range (WS. cells [1, 1], WS. cells [1, Col]);
Range. mergecells = true;
Range. value2 = title;
For (INT I = 1; I <= T. Rows. Count; I ++)
{
Int COUNT = 1; // current column
For (Int J = 1; j <= T. Rows [I-1]. cells. Count; j ++)
{
// Ws. cells [I, j] = "";
Int colspan = T. Rows [I-1]. cells [J-1]. columnspan;
If (colspan = 0) colspan = 1;
If (colspan = 1)
{
WS. cells [I + 1, Count] = cleanstr (T. Rows [I-1]. cells [J-1]. Text );
Range _ r = ws. cells [I + 1, Count] As range;
Excel. Style S = _ R. style as Excel. style;
S. horizontalalignment = xlhalign. xlhaligncenter;
Count ++;
}
Else
{
Excel. Range _ range = ws. get_range (WS. cells [I + 1, Count], WS. cells [I + 1, Count + colspan-1]);
_ Range. mergecells = true;
_ Range. value2 = cleanstr (T. Rows [I-1]. cells [J-1]. Text );
Count = count + colspan;
}

}
}
WB. savecopyas (httpcontext. Current. server. mappath ("~ /Courses/temp/excel.xls "));

}

Protected string cleanstr (string Str)
{
Stringbuilder BS = new stringbuilder (STR );
BS = BS. Replace ("<br/> ","");
Return BS. tostring ();
}

}

 

Current problems:

1. You cannot control the line breaks in Excel cells;

2. generate an excelfile and download it to the client. When using excelediting, two boundaries are displayed: sheet1and excel.xls (for example)

Hope the high people give advice to the maze ....

Related Article

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.