導入Excel模板中的數據到數據庫(按行讀取Excel)
using Aspose.Cells;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiCheng.Common.DAL;
using YiCheng.Common.Sql;
using YiCheng.Common.Tools;
using YiCheng.PreOrder;
using YiCheng.PreOrder.ChanPin;
using YiCheng.Systems.Files;
using YiCheng.Systems.Number;
public string ImportExcel(string path, string orderID)
{
string resultMsg = "";
Cells eCells;
var workbook = new Workbook(path);
try
{
eCells = workbook.Worksheets[0].Cells;
string title = YAsposeCellsHelper.Read(eCells, 0, "*****", 0).ToString();
}
catch
{
throw new Exception("不是***的Excel數據");
}
// 預訂單
YPreOrder EShopOrder = new YPreOrder();
// 讀取最後一行的數據,一般是備註之類的
string remark = Read(eCells, eCells.MaxDataRow, "標題", 0);
EShopOrder.備註 = remark;
// Excel表格行座標是從0開始的
// 處理下部分數據
for (int i = 7; i < eCells.MaxDataRow - 1; i++)
{
// 遇到空數據時跳過後讀取下一條,防止數據不連續時遺漏(要導入的數據安裝位置不能爲空)
if (string.IsNullOrEmpty(Read(eCells, i, "安裝位置")))
{
continue;
}
YPreOrderZuDan P = new YPreOrderZuDan();
try
{
P.Excel行號 = i + 1;//取出表頭標題、同時Excel從1開始計數
// 讀取數據
P.安裝位置 = Read(eCells, i, "安裝位置");
P.洞口類型 = Read(eCells, i, "產品分類");
P.門型號 = Read(eCells, i, "門型號");
P.套型號 = Read(eCells, i, "套型號");
P.線型號 = Read(eCells, i, "線型號");
P.型號 = Read(eCells, i, "型號");
P.樹種 = Read(eCells, i, "飾面材料");
P.油漆顏色 = Read(eCells, i, "色號");
P.銷售單位 = Read(eCells, i, "單位");
P.備註 = Read(eCells, i, "備註");
string 高度 = Read(eCells, i, "高度", K_Row_Title_Index + 1);//跨行
string 寬度 = Read(eCells, i, "寬度", K_Row_Title_Index + 1);
string 牆厚 = Read(eCells, i, "牆厚", K_Row_Title_Index + 1);
P.門洞尺寸 = 高度 + "*" + 寬度 + "*" + 牆厚;
P.數量 = Convert.ToDecimal(Read(eCells, i, "數量"));
string 非標費用 = Read(eCells, i, "非標費用");
if (非標費用.IsEmpty())
{
非標費用 = "0";
}
P.非標價格 = Convert.ToDecimal(非標費用);
string 原始價格 = Read(eCells, i, "原始價格");
if (原始價格.IsEmpty())
{
原始價格 = "0";
}
P.原始零售價格 = Convert.ToDecimal(原始價格);
string 零售價格 = Read(eCells, i, "零售價格");
if (零售價格.IsEmpty())
{
零售價格 = "0";
}
P.零售價格 = Convert.ToDecimal(零售價格);
}
catch (Exception ex)
{
throw new Exception(ex.Message + ",行號:" + P.Excel行號);
}
}
//處理上部分數據
for (int i = 1; i < 5; i++)
{
for (int j = 0; j < eCells.MaxDataColumn + 1; j++)
{
string s = eCells[i, j].StringValue.Trim();
string field = GetFieldValue(s).Item1;
string value = GetFieldValue(s).Item2;
switch (field)
{
case "客戶姓名":
EShopOrder.客戶姓名 = value;
break;
case "訂貨日期":
if (string.IsNullOrEmpty(value))
{
EShopOrder.預訂貨日期 = DateTime.Now;
}
else {
EShopOrder.預訂貨日期 = Convert.ToDateTime(value);
}
break;
case "客戶地址":
EShopOrder.客戶地址 = value;
break;
case "設計師":
EShopOrder.設計師 = value;
break;
case "交貨日期":
if (string.IsNullOrEmpty(value))
{
EShopOrder.預交貨日期 = DateTime.Now;
}
else {
EShopOrder.預交貨日期 = Convert.ToDateTime(value);
}
break;
case "客戶電話":
EShopOrder.客戶電話 = value;
break;
case "設計師電話":
EShopOrder.設計師電話 = value;
break;
case "銷售電話":
EShopOrder.導購員電話 = value;
break;
case "銷售顧問":
EShopOrder.導購員 = value;
break;
}
}
}
// 插入數據庫省略
resultMsg = "數據導入成功 " + EShopZuDanList.Count + " 條!";
return resultMsg;
}
//處理字符串,返回鍵值對
private static Tuple<string, string> GetFieldValue(string field)
{
if (!string.IsNullOrEmpty(field))
{
if (field.Contains(":"))
{
var str = field.Split(":");
var value = str.Count() < 2 ? "" : str[1].Trim();
return new Tuple<string, string>(str[0].Trim(), value);
}
if (field.Contains(":"))
{
var str = field.Split(":");
var value = str.Count() < 2 ? "" : str[1].Trim();
return new Tuple<string, string>(str[0].Trim(), value);
}
throw new Exception($"信息導入有誤,錯誤數據爲:{field}!");
}
return new Tuple<string, string>("", "");
}