导入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>("", "");
}