MVC利用Aspose.Cells.dll實現Excel資料匯入資料庫
阿新 • • 發佈:2018-12-30
/// <summary>匯入Excel資料到資料庫 /// /// </summary> /// <param name="file">檔案</param> public ActionResult ImportExcel(HttpPostedFileBase file) { if (file != null) { bool exportColumnName = true; Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(); workbook.Open(file.FileName); Aspose.Cells.Worksheet worksheet = workbook.Worksheets[0]; Cells cells = worksheet.Cells;DataTable datatable = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, exportColumnName);//這裡用到Aspose.Cells的ExportDataTableAsString方法來讀取excel資料 //對班級進行分組 var groupByClass = datatable.Rows.Cast<DataRow>().GroupBy(u => u["班級"]); //匯入一次資料就清空班級表 string deleteSql = "TRUNCATE TABLE dbo.Classes"; db.Database.ExecuteSqlCommand(deleteSql); foreach (var item in groupByClass) { Classes classes = new Classes(); classes.ClassName = item.Key.ToString(); db.Classes.Add(classes); db.SaveChanges(); } string[] fileds = new string[] { "班級" }; // 分組條件 List<DataTable> grouped = new List<DataTable>(); // 儲存分組結果 //得到分組資料,並存到grouped中 GroupDataRows(datatable.Rows.Cast<DataRow>(), grouped, fileds, 0, datatable); // 輸出分組 foreach (DataTable dt in grouped) { foreach (DataRow row in dt.Rows) { string className = row["班級"].ToString(); int classID = db.Classes.Where(cl => cl.ClassName == className).FirstOrDefault().ID; row.BeginEdit(); row["班級"] = classID; if (row["性別"].ToString() == "男") { row["性別"] = 0; } else { row["性別"] = 1; } User user = new User(); user.UserName = row["賬號"].ToString(); user.Password = row["密碼"].ToString(); user.NickName = row["呢稱"].ToString(); user.RoleID = 4; user.Sex = int.Parse(row["性別"].ToString()); if (row["年齡"].ToString() == "") { user.Age = null; } else { user.Age = int.Parse(row["年齡"].ToString()); } if (row["學校"].ToString() == "") { user.School = null; } else { user.School = row["學校"].ToString(); } if (row["積分"].ToString() == "") { user.Coins = null; } else { user.Coins = int.Parse(row["積分"].ToString()); } user.ClassID = int.Parse(row["班級"].ToString()); db.User.Add(user); db.SaveChanges(); } } return RedirectToAction("StudentIndex"); } else { return RedirectToAction("StudentIndex"); } } /// <summary>把dataTable資料通過某列分組,再把分組資料存到LIst<Datatable>中 /// /// </summary> /// <param name="source">資料中每行資料</param> /// <param name="destination">分組後資料存入的Datatable</param> /// <param name="groupByFields">分組欄位陣列</param> /// <param name="fieldIndex"></param> /// <param name="schema">資料集</param> public static void GroupDataRows(IEnumerable<DataRow> source, List<DataTable> destination, string[] groupByFields, int fieldIndex, DataTable schema) { if (fieldIndex >= groupByFields.Length || fieldIndex < 0) { DataTable dt = schema.Clone(); foreach (DataRow row in source) { DataRow dr = dt.NewRow(); dr.ItemArray = row.ItemArray; dt.Rows.Add(dr); } destination.Add(dt); return; } var results = source.GroupBy(o => o[groupByFields[fieldIndex]]); foreach (var rows in results) { GroupDataRows(rows, destination, groupByFields, fieldIndex + 1, schema); } fieldIndex++; }
public System.Data.DataTable ExportDataTableAsString ( Int32 firstRow, Int32 firstColumn, Int32 totalRows, Int32 totalColumns, Boolean exportColumnName ) |
Name | Description |
---|---|
firstRow | The row number of the first cell to export out. |
firstColumn | The column number of the first cell to export out. |
totalRows | Number of rows to be imported. |
totalColumns | Number of columns to be imported. |
exportColumnName | Indicates whether the data in the first row are exported to the column name of the DataTable |