Web page upload Excel file to server, service end with Npoi parsing Excel

Source: Internet
Author: User
Tags rowcount

aspx

<%@ page language= "C #" autoeventwireup= "true" codefile= "MyLoad.aspx.cs" inherits= "Updateaddi_myload"%><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
Aspx.cs:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;using system.componentmodel;using system.data;using System.Drawing;using System.Linq; Using system.text;using system.data.oledb;using system.io;using agiso.dbaccess;using NPOI. Ss. Usermodel;using Npoi. HSSF. Usermodel;using Npoi. XSSF.    Usermodel;public partial class updateaddi_myload:system.web.ui.page{string serverpath = "";    DataTable data = null;    DataTable olddata = null;    String cellvalue = "";    String DataLabel = ""; protected void Page_Load (object sender, EventArgs e) {} protected void Bt_upload_click (object sender, EventArgs E) {try {if (FileUpload1.PostedFile.FileName = = "") {This.lb_info . Text = "Please select File!"            ";                } else {string filepath = FileUpload1.PostedFile.FileName; string filename = filepath. Substring (FilepatH.lastindexof ("\ \") + 1); Serverpath = Server.MapPath ("..                /files/uploadfile/") + filename;                FileUpload1.PostedFile.SaveAs (Serverpath); This.lb_info.                Text + = "File Path =" + filepath; This.lb_info. Text + = "Upload successful!                ";                data = Getdatafromexcelbynpoi (); This.lb_info.                                Text = ""; Remove all headers for (int i = 0; i < data. Columns.count; i++) {Lb_info. Text + = ("{" + data.                Columns[i] + "}"); } lb_info.                Text + = "\ r \ n"; Lb_info.                Text + = "//---------above is the title, the following is the cell---------------------------"; Lb_info.                Text + = "\ r \ n"; Remove all cells outside the header for (int i = 0; i < data. Rows.Count; i++) {for (int j = 0; j < data. Columns.count; J + +) {if (data. ROWS[I][J].   ToString () = = "") {                         Cellvalue = "------"; } else {cellvalue = data. ROWS[I][J].                        ToString (); } lb_info.                    Text + = ("{" + Cellvalue + "}"); } lb_info.                Text + = "\ r \ n";            }}//Read database OldData = Getorderdata ();  for (int k = 0, K < OldData.Rows.Count; k++) {for (int v = 0; v < oldData.Columns.Count;                            v++) {switch (v) {case 0:                            Datalabel= "numiid=";                        Break                            Case 1:datalabel= "title=";                        Break                            Case 2:datalabel= "Picurl=";                        BreakCase 3:datalabel= "outerid=";                        Break                            Case 4:datalabel= "additional=";                        Break                    Default:break; } lb_info.                    Text+=datalabel; Lb_info. Text + = Olddata.rows[k][v].                    ToString (); Lb_info.                Text + = "\ r \ n"; }}} catch (Exception ex) {this.lb_info. Text = "Upload error occurred!" The reason is: "+ ex."        ToString ();        }} DataTable Getorderdata () {idbaccess db = Dbaccessdal.createdbaccess (); Return DB. Executetable (String. Format ("Select Numiid, Title, Picurl,outerid, Additional from Autodummysenditemdefine WHERE nick={0}",    Dbutil.tosqlstring ("* * *")));        } DataTable Getdatafromexcelbynpoi () {Iworkbook workbook;        if (Serverpath = = "") return null; String fileType = Path.        GetExtension (Serverpath); using (FileStream file = new FileStream (Serverpath, FileMode.Open, FileAccess.Read)) {if (FileType = = ")            . xls ") {workbook = new Hssfworkbook (file);            } else if (FileType = = ". xlsx") {workbook = new Xssfworkbook (file);            } else {return null; } isheet sheet = workbook.            Getsheetat (0);//Take the first table of DataTable table = new DataTable (); IRow HeaderRow = sheet.            GetRow (0);            int cellcount = Headerrow.lastcellnum; int rowCount = sheet.            Lastrownum; for (int i = headerrow.firstcellnum; i < Cellcount; i++) {DataColumn column = new DataColumn (Headerrow.getcell (i).                ToString ()); Table.            Columns.Add (column); } for (int i = (sheet. Firstrownum + 1); I <= RowCount; i++) {IRow row= Sheet.                GetRow (i); DataRow datarow = table.                NewRow (); if (row! = null) {for (int j = row). Firstcellnum; J < Cellcount; J + +) {if (row. Getcell (j) = null) datarow[j] = Getcellvalue (row.                    Getcell (j)); }} table.            Rows.Add (DataRow);        } return table;        }} Private Object Getcellvalue (Icell cell) {object value = NULL; try {if (cell. Celltype! = Celltype.blank) {switch (cell.                        Celltype) {case celltype.numeric://Date comes here if (dateutil.iscelldateformatted (cell)) {value = cell.                        Datecellvalue;                      } else {      Numeric Type value = Cell.                        Numericcellvalue;                    } break; Case Celltype.boolean://Boolean Type value = Cell.                        Booleancellvalue;                    Break Case CellType.Formula:value = cell.                        Cellformula;                    Break Default://String Type value = Cell.                        Stringcellvalue;                Break        }}} catch (Exception) {value = "";    } return value; }}


Web page upload Excel file to server, service end with Npoi parsing Excel

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.