Asp.net Import and Export Excel

Source: Internet
Author: User

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

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.