四 导入
2015-06-03 21:28
267 查看
导入
(1)导入一张Excel表:(图 1)
(图 2)
实现界面的代码:
<html> <head> <script src="../../Scripts/jquery-1.4.4.js" type="text/javascript"></script> <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script> <script type="text/javascript"> function daoru() { window.location.href = "/Main/DownloadFile"; } </script> </head> <body> <input type="button" value="导出" onclick="daoru()"/> <a href="/Main/ExportExcel">Click here</a> <form action="/Main/GetTableFromExcel" method="post" enctype="multipart/form-data"> //调用控制器的方法 <text>选择上传文件</text> <input name="file1" type="file" id="file" /> <input type="submit" name="Upload" value="导入" /> </form> </body> </html>
调用控制器的方法:
#endregion //导入 public ActionResult GetTableFromExcel() { HttpPostedFileBase fostFile = Request.Files["file1"]; Stream streamfile = fostFile.InputStream; HSSFWorkbook hssfworkbook = new HSSFWorkbook(streamfile); using (NPOI.SS.UserModel.ISheet sheet = hssfworkbook.GetSheetAt(0)) { DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(0);//第一行为标题行 int cellCount = headerRow.LastCellNum;//LastCellNum = PhysicalNumberOfCells int rowCount = sheet.LastRowNum;//LastRowNum = PhysicalNumberOfRows - 1 //handling header. for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); 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); } for (int i = 0; i < table.Rows.Count; i++) { myUpLoadBLL.ForDownLoad(table.Rows[i][1].ToString(), table.Rows[i][2].ToString(),Convert.ToBoolean( table.Rows[i][3])); //调用逻辑层的方法 } } return Content(""); } /// <summary> /// 根据Excel列类型获取列的值 /// </summary> /// <param name="cell">Excel列</param> /// <returns></returns> private static string GetCellValue(ICell cell) { if (cell == null) return string.Empty; switch (cell.CellType) { case CellType.BLANK: return string.Empty; case CellType.BOOLEAN: return cell.BooleanCellValue.ToString(); case CellType.ERROR: return cell.ErrorCellValue.ToString(); case CellType.NUMERIC: case CellType.Unknown: default: return cell.ToString(); case CellType.STRING: return cell.StringCellValue; case CellType.FORMULA: try { HSSFFormulaEvaluator e = new HSSFFormulaEvaluator(cell.Sheet.Workbook); e.EvaluateInCell(cell); return cell.ToString(); } catch { return cell.NumericCellValue.ToString(); } } }
逻辑层的方法:
#region 导入 public int ForDownLoad(string Name,string Age,bool Gender) { //创建数组 SqlParameter[] sql ={ //实例化并传入参数和参数类型 new SqlParameter("@type",SqlDbType.Char), new SqlParameter("@Name",SqlDbType.NVarChar), new SqlParameter("@Age",SqlDbType.NVarChar), new SqlParameter("@Gender",SqlDbType.Bit), }; //为参数赋值 sql[0].Value = "ForDownLoad"; sql[1].Value = Name; sql[2].Value = Age; sql[3].Value = Gender; //调用DAL方法连接数据库提取数据存放到数据表dt中 int count = myPublicMoth.UpdateData("ForUpLoadAndDownLoad", sql); //返回dt return count; } #endregion
数据层的SQL语句:
@type char(100)='', @Name varchar(50)='', @Age varchar(10)='', @Gender bit=0 if(@type='ForDownLoad') begin Insert into Person(Name,Age,Gender) VALUES(@Name,@Age,@Gender) End
只供学习参考!
相关文章推荐
- (1)基本运算+复数+字符串
- ORACLE 11G在同一台linux服务器从实例1全库导入到实例2上
- HDU1249 三角形 递推
- [Android Studio 权威教程]Linux下安装Android Studio
- ProgressDialog使用总结
- 深入理解C++中的mutable关键字
- 80x86寻址方式 -- 实模式和保护模式
- Reorder List - LeetCode 143
- C#构造函数的声明、重载与继承调用
- uva 11584 Partitioning by Palindromes 线性dp
- 快速排序算法
- JavaScript性能优化小知识总结
- 花指令
- Zend Studio配合Xdebug调试
- TCP/IP------DNS、http、Telnet
- POJ 1182 食物链(并查集较高级的应用)
- log4j DatePattern 解惑
- MFC线程间发送消息
- 143. Reorder List
- iOS微信接入错误解决。~