Excel Data Import ___ do you hold it (1), data import hold

Source: Internet
Author: User

Excel Data Import ___ do you hold it (1), data import hold

Recently, I developed the ITOO3.0 University cloud platform project along with my team. The gains are self-evident. There is an import function in the project: Import student information; import class information: Import teaching building information, etc, after several attempts, the function is successfully implemented.

Framework Analysis

For more information, see
-Step 1: Create a folder on the Client side of the MVC Framework
-Step 2: Use the NPOI file stream (the specific professional name does not know what it is called) to spread the Excel files stored in the Client to the Server of the WCF framework.
-Step 3: On the Server side, create a folder to save the xml file of the Excel template

The XML file in the Server parses the data and inserts the data in Excel into the data table.

Code Design

Upload an Excel file to the Server
* Controller

Private readonly IUploadFile upLoadService1 = ServiceFactory. GetUploadFileService ();
# Region ImportFlowBatch () + batch import process-Xu Lu-July 8, 2015 10:39:02
/// Batch import process
Public ActionResult ImportFlowBatch ()
# Verify the region file and upload it to the Client of the specified folder
HttpPostedFileBase file = Request. Files ["files"];
String strFileName;
String strSavePath;
String ClientPath = AppDomain. CurrentDomain. BaseDirectory + "File \ UpFile \";
String strPaperId = "1 ";
// This is the file storage path of the Client.

If (file = null | file. contentLength <= 0) {ViewBag. error = "file cannot be blank"; return View ();} else {string strFilename = Path. getFileName (file. fileName); int intFilesize = file. contentLength; // obtain the size of the uploaded file. The unit is byte string fileEx = System. IO. path. getExtension (strFilename); // get the extension string strNoFileName = System. IO. path. getFileNameWithoutExtension (strFilename); // get the file name without an extension int Maxsize = 4000*1024; // define The maximum size of the uploaded file is 4 M string FileType = ".xls,.xlsx"; // defines the type string strFileName = strNoFileName + DateTime of the uploaded file. now. toString ("yyyyMMddhhmmss") + fileEx; if (! FileType. contains (fileEx) {ViewBag. error = "the file type is incorrect. Only files in xls and xlsx formats can be imported"; // return View ();} if (intFilesize> = Maxsize) {ViewBag. error = "Upload file exceeds 4 MB and cannot be uploaded"; // return View ();} strSavePath = Path. combine (ClientPath, strFileName); file. saveAs (strSavePath) ;}# endregion # region uploads files uploaded from the Client to the Server-side FileUploadMessage myFileMessage = new FileUploadMessage (); string strDataFileName = file. fileName; myFileMessage. fileName = strDataFileName; // file name string CientPathName = ClientPath + strFileName; using (FileStream fs = System. IO. file. openRead (CientPathName) {myFileMessage. fileData = fs; try {upLoadService1.UploadFileMethod (myFileMessage);} catch {}// close stream fs. close () ;}# endregion string [] HeadName = {"process ID", "process name", "Priority", "Whether to enable (enable 1, not enabled 0 )", "process Url", "timestamp", "Delete?"}; // call and execute write database if (upLoadService1.ServiceReadFile (strDataFileName, strPaperId) = null) {return RedirectToAction ("Index", "ImportStudent");} else {DataTable table = upLoadService1. serviceReadFile (strDataFileName, strPaperId) [0]; return File (Export. exportManager. exportExcel (table, HeadName), "application/vnd. ms-excel "," process import Error List "+ ". xls ") ;}# endregion

* Read an Excel file from the Service and write it to the database.
Three to be added: IUploadFile (Interface), UploadFile (Implementation class), and ServiceFactory (factory)
Space limit. The specific code will not be pasted. The mini-editor will upload the Demo and download it separately.

* XML configuration
An XML configuration file is required for each imported Excel file, so that the underlying method can be parsed and obtained.
XML should be stored in a specific path. The specific path configuration is as follows:
Add a configuration node in WebConfig. xml:
Add nodes below

Place the path of the configuration file in the Models/ImportConfigXML folder of the Service layer.
Naming: each system can be named based on specific conditions. When calling a method, you must input an XML file name.
For detailed XML configuration, refer:

<? Xml version = "1.0" encoding = "UTF-8"?> <Excel name = "import process template"> <Sheet name = "process" table = "FreshFlowEntity" primaryKey = "FlowID" pkType = "guid"> <Column name = "process name ""field =" Name "> <DataType> string </DataType> <ForeignKey isExist =" false "> </ForeignKey> </Column> <Column name =" Priority "field = "Sort" isNecessary = "true"> <DataType> int </DataType> <ForeignKey isExist = "false"> </ForeignKey> </Column> <Column name = "enable? (enable 1, 0 is not enabled) "field =" IsUse "isVerifyRepeat =" false "> <DataType> int </DataType> <ForeignKey isExist =" false "> </ForeignKey> </Column> <Column name = "process Url" field = "Url" isVerifyRepeat = "false"> <DataType> string </DataType> <ForeignKey isExist = "false"> </ForeignKey> </Column> <column name = "timestamp" field = "TimeSpan" isNecessary = "false" isVerifyRepeat = "false"> <DataType> DateTime </DataType> <ForeignKey isExist = "false"> </ForeignKey> </Column> <Column name = "delete" field = "IsDelete" isVerifyRepeat = "false"> <DataType> int </DataType> <ForeignKey isExist = "false "> </ForeignKey> </Column> </Sheet> </Excel>

The above is the basic process for importing data. Is it possible to run the data properly after the code is knocked out?

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.