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 ....