Export:
Import Template
String filepath = server. mappath ("~ /Templatefile/pay-as-you-go template.xls ");
String newpath = server. mappath ("~ /Templatefile/"+ guid. newguid () +". xls ");
File. Copy (filepath, newpath );
File. setattributes (newpath, fileattributes. Normal );
Stream S = file. Open (newpath, filemode. Open );
// ---------- Create an Excel file
Hssfworkbook WB = new npoi. hssf. usermodel. hssfworkbook (s );
S. Close ();
File. Delete (newpath );
Hssfsheet sheet = WB. getsheetat (0 );
String stradd = string. empty;
String strnull = string. empty;
Int rowcount = sheet. lastrownum-1;
Stringbuilder sberror = new stringbuilder ();
Hssfcell cell;
String where = "and dealed = 1 and auditingstate = 'finished' and rewardtype = 'day'" + strwhere;
Datatable dt = projecusertdal. getparttimereward_wflog (where );
// Create a row
Hssfrow row2 = sheet. getrow (3 );
If (row2 = NULL)
Row2 = new hssfrow (WB, sheet, 3 );
If (row2! = NULL)
{
String pid = request ["PID"];
If (PID! = NULL)
{
// Obtain a cell
Hssfcell cell2 = row2.getcell (1 );
Cell2.setcellvalue (projectno );
Hssfcell cell6 = row2.getcell (6 );
Cell6.setcellvalue (projectname );
Hssfcell cell9 = row2.getcell (9 );
Cell9.setcellvalue (date );
Hssfcell cell12 = row2.getcell (12 );
Cell12.setcellvalue (request ["city"]);
# Download region output Excel
WB. Write (response. outputstream );
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename ("pay-as-you-go template.xls"), system. Text. encoding. utf8 ));
Response. contenttype = "application/octet-stream ";
Response. Flush ();
Response. End ();
# Endregion
Import:
// Obtain the import path
String filepath = path. Combine (request. physicalapplicationpath, String. Format ("Temp \ {0}", fileupload1.filename ));
Stream S = file. Create (filepath );
S. Write (fileupload1.filebytes, 0, fileupload1.filebytes. Length );
// Create an Excel file
Hssfworkbook WB = new npoi. hssf. usermodel. hssfworkbook (s );
Hssfsheet sheet = WB. getsheetat (0 );
String stradd = string. empty;
String strnull = string. empty;
Int rowcount = sheet. lastrownum-1;
Stringbuilder sberror = new stringbuilder ();
For (INT I = 2; I <rowcount; I ++)
{
Hssfrow ROW = sheet. getrow (I );
Strnull = checkvalid (ROW );
If (string. isnullorempty (strnull) = false)
{
Sberror. append (string. format ("<tr> <TD style = 'cursor: hand; 'onclick = \" document. all. r {0 }. style. display = document. all. r {0 }. style. display = ''? 'None': ''; \"> failed to import row {0! <Div id = 'r {0} 'style = 'display: none'> cause: {1} </div> </TD> </tr>"
, I + 1, strnull ));
Continue;
}
Else
{
Stradd = adddata (ROW );
If (string. isnullorempty (stradd) = false)
{
Sberror. append (string. format ("<tr> <TD style = 'cursor: hand; 'onclick = \" document. all. r {0 }. style. display = document. all. r {0 }. style. display = ''? 'None': ''; \"> failed to import row {0! <Div id = 'r {0} 'style = 'display: none'> cause: {1} </div> </TD> </tr>"
, I + 1, stradd ));
}
}
Stradd = string. empty;
Strnull = string. empty;
}
S. Close ();
File. Delete (filepath );
If (sberror. Length <1)
{
Labmsg. Text = "Import successful! ";
}
Else
{
Labmsg. Text = string. Format ("<Table class = 'errormsg '> {0} </table>", sberror. tostring ());
}
Control: http://www.openso.net/downtool? Id = 264