SVN r735
SVN-Revision: r735
This commit is contained in:
@@ -39,6 +39,9 @@ using System.Diagnostics;
|
||||
using DevExpress.XtraGrid.Views.BandedGrid;
|
||||
using DevExpress.Utils;
|
||||
using Lskj.Control;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using NPOI.HSSF.UserModel;
|
||||
using DevExpress.XtraGrid.Views.Grid;
|
||||
|
||||
namespace Lskj.PubBomImportNew
|
||||
{
|
||||
@@ -96,6 +99,10 @@ namespace Lskj.PubBomImportNew
|
||||
/// </summary>
|
||||
public GridControlEx ParentGridEx;
|
||||
|
||||
/// <summary>
|
||||
/// 缓存列对象
|
||||
/// </summary>
|
||||
private static List<GridColumnModel> ColumnList = new List<GridColumnModel>();
|
||||
/// <summary>
|
||||
/// 源表格
|
||||
/// </summary>
|
||||
@@ -108,17 +115,20 @@ namespace Lskj.PubBomImportNew
|
||||
/// 导入返回控件值
|
||||
/// </summary>
|
||||
public List<string> ImportReturnValue = new List<string>();
|
||||
public int FirstRowIndex = 0;
|
||||
protected override void OnLoad(EventArgs e)
|
||||
{
|
||||
DataRow modelRow = MainImpl.GetSystemdllTab(Model.ModuleCode);//获取模块信息
|
||||
int.TryParse(Model.stratRowHand + "", out FirstRowIndex);
|
||||
DataRow modelRow = MainImpl.GetSystemdllTab(Model.ModuleCode);//获取模块信息
|
||||
if (modelRow != null)
|
||||
{
|
||||
this.SysModel = new ModuleModel(modelRow);
|
||||
InitializeControl(this.SysModel.MenuTable, this.Model.ModuleCode, _parmaryKey, this.gcMain, this.SysModel.PrefixKey, this.Model.primkey, this.Model.keyvalue, this.Model.FormText, this.SysModel.ConcatenatedPrefix);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
MessageUtil.Show("请检查模板是否配置正确!");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +137,7 @@ namespace Lskj.PubBomImportNew
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
|
||||
this._tableColumns = BaseModuleImpl.GetBaseGridColumns(menuCode);
|
||||
this._tableName = tableName;
|
||||
this._menuCode = menuCode;
|
||||
@@ -163,7 +173,6 @@ namespace Lskj.PubBomImportNew
|
||||
string Message = ErrorMessage.PromptErrorMessage(ex);
|
||||
MessageUtil.Show(Message, ex.Message);
|
||||
}
|
||||
Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -305,49 +314,31 @@ namespace Lskj.PubBomImportNew
|
||||
}
|
||||
}
|
||||
}
|
||||
public string GetValueByImportText(DataRow row, string fieldName, string text)
|
||||
/// <summary>
|
||||
/// 获取导入到下拉框里的显示值
|
||||
/// </summary>
|
||||
/// <param name="row"></param>
|
||||
/// <param name="fieldName"></param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="isBillImport"></param>
|
||||
/// <returns></returns>
|
||||
public string GetValueByImportText(DataRow row, string fieldName, string text, bool isBillImport = false)
|
||||
{
|
||||
string value = text;
|
||||
foreach (GridColumnModel model in this.ValueGridColumnTable.Keys)
|
||||
foreach (GridColumnModel model in ValueGridColumnTable.Keys)
|
||||
{
|
||||
if (model.FieldName.Equals(fieldName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string selWhere = "1=1";
|
||||
DataTable table = this.ValueGridColumnTable[model];
|
||||
if (model.FieldType == ControlType.LabMultiSelectValue || model.FieldType == ControlType.LabMultiSelectValueNew || model.FieldType == ControlType.LabMultiSelectValueParam)
|
||||
DataTable table = ValueGridColumnTable[model];
|
||||
if (table != null && table.Rows.Count > 0)
|
||||
{
|
||||
string[] splitString = text.Split(',');
|
||||
string nemValue = string.Empty;
|
||||
foreach (string newText in splitString)
|
||||
{
|
||||
if (table != null && table.Rows.Count > 0)
|
||||
{
|
||||
DataRow rowItemValue = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.TextMember] + "" == newText);
|
||||
DataRow rowItemText = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.ValueMember] + "" == newText);
|
||||
value = rowItemValue != null ? rowItemValue[model.ValueMember] + "" : rowItemText != null ? rowItemText[model.ValueMember] + "" : "";
|
||||
}
|
||||
else
|
||||
{
|
||||
value = "";
|
||||
}
|
||||
nemValue += value + ',';
|
||||
}
|
||||
value = nemValue.TrimEnd(',');
|
||||
DataRow rowItemValue = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.TextMember] + "" == text);
|
||||
DataRow rowItemText = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.ValueMember] + "" == text);
|
||||
value = rowItemValue != null ? rowItemValue[isBillImport ? model.ValueMember : model.TextMember] + "" : rowItemText != null ? rowItemText[isBillImport ? model.ValueMember : model.TextMember] + "" : "";
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (table != null && table.Rows.Count > 0)
|
||||
{
|
||||
DataRow rowItemValue = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.TextMember] + "" == text);
|
||||
DataRow rowItemText = table.Rows.Cast<DataRow>().FirstOrDefault(x => x[model.ValueMember] + "" == text);
|
||||
value = rowItemValue != null ? rowItemValue[model.ValueMember] + "" : rowItemText != null ? rowItemText[model.ValueMember] + "" : "";
|
||||
}
|
||||
else
|
||||
value = "";
|
||||
|
||||
}
|
||||
|
||||
value = "";
|
||||
}
|
||||
}
|
||||
return value;
|
||||
@@ -866,6 +857,19 @@ namespace Lskj.PubBomImportNew
|
||||
{
|
||||
try
|
||||
{
|
||||
if (BaseImpl.HasExistsTable("P_SpecialImportTab"))
|
||||
{
|
||||
string searchSql = string.Format("select * from P_SpecialImportTab where moduleId = '{0}' and rightMenuName = '{1}'", Model.ModuleCode, Model.ModuleId);
|
||||
DataTable dataTable = SqlHelper.ExecuteDataTable(searchSql);
|
||||
if (dataTable == null || dataTable.Rows.Count == 0)
|
||||
{
|
||||
MessageUtil.Show("未配置关联\r\n请联系管理员进行配置");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageUtil.Show("未配置关联\r\n请联系管理员进行配置");
|
||||
}
|
||||
OpenFileDialog dialog = new OpenFileDialog();
|
||||
dialog.Title = "选择导入文件";
|
||||
// dialog.Filter = "Excel|*.xls|Excel|*.xlsx";
|
||||
@@ -882,7 +886,9 @@ namespace Lskj.PubBomImportNew
|
||||
//bool isMultipleHeader = false;
|
||||
//if (this._gridEx is BandedGridControlEx) isMultipleHeader = true;
|
||||
//this.gcMain.GridView.Tag = isMultipleHeader;
|
||||
gcData = this.gcMain.GridView.ToExcelDataTable(fileName, colFields, true);
|
||||
|
||||
gcData = ToExcelDataTable(fileName, out int count, out int errorCount, true);
|
||||
|
||||
|
||||
|
||||
//设置默认值
|
||||
@@ -990,8 +996,8 @@ namespace Lskj.PubBomImportNew
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
@@ -1015,9 +1021,17 @@ namespace Lskj.PubBomImportNew
|
||||
MessageUtil.Show(Message, ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设计关联
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OnBtnCoverClick(object sender, EventArgs e)
|
||||
{
|
||||
FrmMain frmMain = new FrmMain();
|
||||
frmMain.Model = Model;
|
||||
frmMain.ShowDialog();
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断导入条件
|
||||
/// </summary>
|
||||
@@ -1097,3 +1111,810 @@ namespace Lskj.PubBomImportNew
|
||||
string[] fields = model.UnionFields.Trim(',').Split(',');
|
||||
DataRow rowResult = BaseImpl.GetDataRowResult(unionValues);
|
||||
if (rowResult != null)
|
||||
{
|
||||
for (int i = 0; i < fields.Length; i++)
|
||||
{
|
||||
string field = fields[i];
|
||||
string value = rowResult.Table.Columns.Contains(field) ? rowResult[field] + "" : null;
|
||||
rowItem[field] = value;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 设置为空
|
||||
for (int i = 0; i < fields.Length; i++)
|
||||
{
|
||||
string field = fields[i];
|
||||
rowItem[field] = DBNull.Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteError(ex);
|
||||
LogUtil.WriteError("计算列关联字段--错误-->" + unionValues, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取表格数据
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <param name="isFirstRowColumn"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable ExcelToDatatable(string fileName, DynamicBomImportNew Model, string sheetName = "", int firstRowIndex = 0)
|
||||
{
|
||||
DataTable data = new DataTable();
|
||||
FileStream fs;
|
||||
IWorkbook workbook = null;
|
||||
try
|
||||
{
|
||||
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
|
||||
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
|
||||
{
|
||||
workbook = new XSSFWorkbook(fs);
|
||||
}
|
||||
else if (fileName.IndexOf(".xls") > 0) // 2003版本
|
||||
{
|
||||
workbook = new HSSFWorkbook(fs);
|
||||
}
|
||||
ISheet sheet;
|
||||
if (!string.IsNullOrEmpty(sheetName))
|
||||
{
|
||||
sheet = workbook.GetSheet(sheetName);//根据给定的sheet名称获取数据
|
||||
}
|
||||
else
|
||||
{
|
||||
//也可以根据sheet编号来获取数据
|
||||
sheet = workbook.GetSheetAt(0);//获取第几个sheet表(此处表示如果没有给定sheet名称,默认是第一个sheet表)
|
||||
}
|
||||
if (sheet != null)
|
||||
{
|
||||
int startRow = firstRowIndex + 1;
|
||||
IRow firstRow = sheet.GetRow(firstRowIndex);
|
||||
IRow bandRow = null;
|
||||
int cellCount = firstRow.LastCellNum;
|
||||
for (int i = firstRow.FirstCellNum; i < firstRow.LastCellNum; i++)//第一行列数循环
|
||||
{
|
||||
ICell titleCell = firstRow.GetCell(i);
|
||||
ICell bandCell = null;
|
||||
bool isBandTitle = false;
|
||||
if (titleCell.IsMergedCell)
|
||||
{
|
||||
CellRangeAddress mergrRange = FindMergedRegion(sheet, firstRowIndex, i);
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
if (mergrRange != null)
|
||||
{
|
||||
if (mergrRange.FirstColumn < mergrRange.LastColumn)//&& !Model.CustomerServiceApp
|
||||
{
|
||||
isBandTitle = true;
|
||||
bandRow = sheet.GetRow(mergrRange.LastRow + 1);
|
||||
titleCell = firstRow.GetCell(mergrRange.FirstColumn);
|
||||
bandCell = bandRow.GetCell(i);
|
||||
CellRangeAddress bandMergrRange = FindMergedRegion(sheet, mergrRange.LastRow + 1, i);
|
||||
if (bandMergrRange != null)
|
||||
{
|
||||
startRow = bandMergrRange.LastRow + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
string fieldName = "";
|
||||
if (!isBandTitle || bandRow == null)
|
||||
{
|
||||
fieldName = GetCellValue(titleCell);
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldName = $"{GetCellValue(titleCell)}|{GetCellValue(bandCell)}";
|
||||
}
|
||||
if (string.IsNullOrEmpty(fieldName) && titleCell.IsMergedCell)
|
||||
{
|
||||
cellCount -= 1;
|
||||
continue;
|
||||
}
|
||||
DataColumn column = new DataColumn(fieldName);//获取标题
|
||||
data.Columns.Add(column);//添加列
|
||||
}
|
||||
//最后一行的标号
|
||||
int rowCount = sheet.LastRowNum;
|
||||
for (int i = startRow; i <= rowCount; i++)//循环遍历所有行
|
||||
{
|
||||
IRow row = sheet.GetRow(i);//第几行
|
||||
if (row == null)
|
||||
{
|
||||
continue; //没有数据的行默认是null;
|
||||
}
|
||||
//将excel表每一行的数据添加到datatable的行中
|
||||
DataRow dataRow = data.NewRow();
|
||||
int dataRowIndex = 0;
|
||||
for (int j = firstRow.FirstCellNum; j < cellCount; j++)
|
||||
{
|
||||
ICell cell = row.GetCell(j);
|
||||
if (cell != null && cell.IsMergedCell)
|
||||
{
|
||||
CellRangeAddress mergrRange = FindMergedRegion(sheet, i, j);
|
||||
if (j == mergrRange.FirstColumn)
|
||||
{
|
||||
dataRow[dataRowIndex] = GetCellValue(row.GetCell(j));
|
||||
dataRowIndex += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null
|
||||
{
|
||||
dataRow[dataRowIndex] = GetCellValue(row.GetCell(j));
|
||||
dataRowIndex += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
data.Rows.Add(dataRow);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
throw new IOException(string.Format("读取模板内容失败,原因:{0}", ex.Message));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception(string.Format("读取模板内容失败,原因:{0}", ex.Message));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取合并区域
|
||||
/// </summary>
|
||||
/// <param name="sheet"></param>
|
||||
/// <param name="rowIndex"></param>
|
||||
/// <param name="columnIndex"></param>
|
||||
/// <returns></returns>
|
||||
private CellRangeAddress FindMergedRegion(ISheet sheet, int rowIndex, int columnIndex)
|
||||
{
|
||||
int mergedRegionsCount = sheet.NumMergedRegions;
|
||||
|
||||
for (int i = 0; i < mergedRegionsCount; i++)
|
||||
{
|
||||
CellRangeAddress mergedRegion = sheet.GetMergedRegion(i);
|
||||
if (mergedRegion.IsInRange(rowIndex, columnIndex))
|
||||
{
|
||||
return mergedRegion; // 返回合并区域
|
||||
}
|
||||
}
|
||||
return null; // 未找到合并区域
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取单元格值
|
||||
/// </summary>
|
||||
/// <param name="cell"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetCellValue(ICell cell)
|
||||
{
|
||||
string cellValue = "";
|
||||
try
|
||||
{
|
||||
if (cell != null)
|
||||
{
|
||||
// 根据单元格类型获取值
|
||||
switch (cell.CellType)
|
||||
{
|
||||
case CellType.String:
|
||||
cellValue = cell.StringCellValue;
|
||||
break;
|
||||
case CellType.Numeric:
|
||||
cellValue = cell.NumericCellValue.ToString();
|
||||
break;
|
||||
case CellType.Boolean:
|
||||
cellValue = cell.BooleanCellValue.ToString();
|
||||
break;
|
||||
case CellType.Formula://单元格中带有公式,根据类型继续判断
|
||||
switch (cell.CachedFormulaResultType)
|
||||
{
|
||||
case CellType.String:
|
||||
cellValue = cell.StringCellValue;
|
||||
break;
|
||||
case CellType.Numeric:
|
||||
cellValue = cell.NumericCellValue.ToString();
|
||||
break;
|
||||
case CellType.Boolean:
|
||||
cellValue = cell.BooleanCellValue.ToString();
|
||||
break;
|
||||
case CellType.Blank:
|
||||
break;
|
||||
default:
|
||||
cellValue.ToString();
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
cellValue = cell.ToString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
return cellValue;
|
||||
}
|
||||
/// <summary>
|
||||
/// 导入到表格中
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="colFields"></param>
|
||||
/// <param name="isDirectImport"></param>
|
||||
/// <param name="isBillImport"></param>
|
||||
/// <returns></returns>
|
||||
public DataTable ToExcelDataTable(string fileName, out int count, out int errorCount, bool isDirectImport = false, bool isBillImport = false)
|
||||
{
|
||||
count = 0;
|
||||
errorCount = 0;
|
||||
GridView gridView = gcMain.GridView;
|
||||
|
||||
DataTable columnsTable = new DataTable();
|
||||
string searchSql = string.Format("select * from P_SpecialImportTab where moduleId = '{0}' and rightMenuName = '{1}'", Model.ModuleCode, Model.ModuleId);
|
||||
DataTable dataTable = SqlHelper.ExecuteDataTable(searchSql);
|
||||
if (dataTable != null && dataTable.Rows.Count > 0)
|
||||
{
|
||||
DataRow dataRow = dataTable.Rows[0];
|
||||
string unionColumn = dataRow["Ls_UnionColumn"] + "";
|
||||
List<string> unionColumnList = unionColumn.Split(',').ToList();
|
||||
columnsTable = GetSourceTable(unionColumnList);
|
||||
}
|
||||
|
||||
DataTable table = gridView.GridControl.DataSourceTable();
|
||||
|
||||
DataTable sourceDataTable = ExcelToDatatable(fileName, Model, "", FirstRowIndex);
|
||||
DataView filterDataView = new DataView(sourceDataTable);
|
||||
try
|
||||
{
|
||||
//if (!string.IsNullOrEmpty(Model.ReadTableCond))
|
||||
//{
|
||||
// filterDataView.RowFilter = Model.ReadTableCond;
|
||||
//}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
}
|
||||
List<DataRow> dataRows = columnsTable.Rows.Cast<DataRow>().Where(n => !string.IsNullOrEmpty(n["excelFieldName"] + "") && !string.IsNullOrEmpty(n["clientFieldName"] + "")).ToList();
|
||||
int index = gridView is BandedGridView ? FirstRowIndex + 1 : FirstRowIndex;
|
||||
isBillImport = index == FirstRowIndex ? true : isBillImport;
|
||||
string fileExt = Path.GetExtension(fileName).ToLower();
|
||||
|
||||
int CurrentRow = 0;//当前行数
|
||||
int CurrentColumn = 0;//当前列数
|
||||
string columnName = string.Empty;//当前列名
|
||||
bool ToAssignment = false;//是否在给新表赋值
|
||||
|
||||
try
|
||||
{
|
||||
ISheet sheet = ExcelHelper.GetSheet(fileName);
|
||||
IRow firstRow = sheet.GetRow(index);
|
||||
IRow bandRow = null;
|
||||
int startRow = FirstRowIndex + 1;
|
||||
string bandTitel = string.Empty;
|
||||
ColumnList.Clear();
|
||||
int cellCount = firstRow.LastCellNum;
|
||||
// 添加列
|
||||
for (int i = firstRow.FirstCellNum; i < firstRow.LastCellNum; i++)
|
||||
{
|
||||
ICell titleCell = firstRow.GetCell(i);
|
||||
ICell bandCell = null;
|
||||
bool isBandTitle = false;
|
||||
if (titleCell.IsMergedCell)
|
||||
{
|
||||
CellRangeAddress mergrRange = FindMergedRegion(sheet, index, i);
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
if (mergrRange != null)
|
||||
{
|
||||
if (mergrRange.FirstColumn < mergrRange.LastColumn)//&& !Model.CustomerServiceApp
|
||||
{
|
||||
isBandTitle = true;
|
||||
bandRow = sheet.GetRow(mergrRange.LastRow + 1);
|
||||
titleCell = firstRow.GetCell(mergrRange.FirstColumn);
|
||||
bandCell = bandRow.GetCell(i);
|
||||
CellRangeAddress bandMergrRange = FindMergedRegion(sheet, mergrRange.LastRow + 1, i);
|
||||
if (bandMergrRange != null)
|
||||
{
|
||||
startRow = bandMergrRange.LastRow + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
string headerName = "";
|
||||
if (!isBandTitle || bandRow == null)
|
||||
{
|
||||
headerName = GetCellValue(titleCell);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(GetCellValue(bandCell)))
|
||||
{
|
||||
headerName = $"{GetCellValue(titleCell)}|{GetCellValue(bandCell)}";
|
||||
}
|
||||
if (string.IsNullOrEmpty(headerName))
|
||||
{
|
||||
cellCount -= 1;
|
||||
continue;
|
||||
}
|
||||
DataRow row = columnsTable.Rows.Cast<DataRow>().Where(x => (x["excelFieldName"] + "").Equals(headerName)).FirstOrDefault();
|
||||
headerName = row != null ? row["clientFieldName"] + "" : headerName;
|
||||
if (string.IsNullOrWhiteSpace(headerName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
GridColumn col = gridView.Columns.OfType<GridColumn>().FirstOrDefault(x => x.FieldName.Equals(headerName));
|
||||
if (col != null)
|
||||
{
|
||||
if (!table.Columns.Contains(col.FieldName))
|
||||
{
|
||||
table.Columns.Add(col.FieldName, col.ColumnType);
|
||||
}
|
||||
ColumnList.Add(col.Tag as GridColumnModel);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageUtil.Show("未找到列->" + headerName + "");
|
||||
}
|
||||
}
|
||||
InitializeValueGrid();
|
||||
bool AutoImportCal = SystemInfo.Instance.AutoImportCal;//是否执行计算公式
|
||||
for (int i = startRow; i <= sheet.LastRowNum; i++)
|
||||
{
|
||||
ToAssignment = true;
|
||||
CurrentRow = i;
|
||||
DataRow rowSource = sourceDataTable.Rows[i - startRow];
|
||||
if (!filterDataView.Cast<DataRowView>().Any(rowView => rowView.Row == rowSource))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
GridColumn col = new GridColumn();
|
||||
IRow row = sheet.GetRow(i);
|
||||
if (row == null) continue;
|
||||
bandTitel = string.Empty;
|
||||
DataRow dataRow = table.NewRow();
|
||||
//if (i == sheet.LastRowNum && fileExt == ".xlsx") break;
|
||||
for (int j = row.FirstCellNum; j < firstRow.LastCellNum; j++)
|
||||
{
|
||||
CurrentColumn = j;
|
||||
ICell cell = row.GetCell(j);
|
||||
ICell titleCell = firstRow.GetCell(j);
|
||||
ICell bandCell = null;
|
||||
bool isBandTitle = false;
|
||||
if (titleCell.IsMergedCell)
|
||||
{
|
||||
CellRangeAddress mergrRange = FindMergedRegion(sheet, index, j);
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
if (mergrRange != null)
|
||||
{
|
||||
if (mergrRange.FirstColumn < mergrRange.LastColumn)//&& !Model.CustomerServiceApp
|
||||
{
|
||||
isBandTitle = true;
|
||||
bandRow = sheet.GetRow(mergrRange.LastRow + 1);
|
||||
titleCell = firstRow.GetCell(mergrRange.FirstColumn);
|
||||
bandCell = bandRow.GetCell(j);
|
||||
CellRangeAddress bandMergrRange = FindMergedRegion(sheet, mergrRange.LastRow + 1, j);
|
||||
if (bandMergrRange != null)
|
||||
{
|
||||
startRow = bandMergrRange.LastRow + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 2;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
startRow = mergrRange.LastRow + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isBandTitle || bandRow == null)
|
||||
{
|
||||
columnName = GetCellValue(titleCell);
|
||||
}
|
||||
else if (!string.IsNullOrEmpty(GetCellValue(bandCell)))
|
||||
{
|
||||
columnName = $"{GetCellValue(titleCell)}|{GetCellValue(bandCell)}";
|
||||
}
|
||||
if (string.IsNullOrEmpty(columnName))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
DataRow colDataRow = dataRows.Where(n => (n["excelFieldName"] + "").Equals(columnName)).FirstOrDefault();
|
||||
if (colDataRow == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
columnName = colDataRow["clientFieldName"] + "";
|
||||
//if (!bandTitel.Equals(Bandcell + "") && !string.IsNullOrEmpty(Bandcell + "")) bandTitel = Bandcell + "";
|
||||
col = gridView.Columns.OfType<GridColumn>().FirstOrDefault(x => x.FieldName.Equals(columnName));
|
||||
if (col == null) continue;
|
||||
GridColumnModel model = col.Tag as GridColumnModel;
|
||||
if (cell != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (cell.CellType == CellType.Blank)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (cell.CellType == CellType.Numeric && (cell + "").StartsWith("-"))
|
||||
{
|
||||
dataRow[col.FieldName] = cell.NumericCellValue;
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cell.CellType == CellType.Numeric)
|
||||
{
|
||||
|
||||
//GridColumnModel model = col.Tag as GridColumnModel;
|
||||
if (model != null && isDirectImport && (model.FieldType == ControlType.LabDate ||
|
||||
model.FieldType == ControlType.LabDateTime ||
|
||||
model.FieldType == ControlType.LabDateTimeShort ||
|
||||
model.FieldType == ControlType.LabTime ||
|
||||
model.FieldType == ControlType.LabShortTime))
|
||||
{
|
||||
string fieldValue = ToDateTimeValue(cell.NumericCellValue + "");
|
||||
if (fieldValue != "")
|
||||
{
|
||||
dataRow[col.FieldName] = Convert.ToDateTime(fieldValue);
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dataRow[col.FieldName] = cell.NumericCellValue;
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//GridColumnModel model = col.Tag as GridColumnModel;
|
||||
if (model != null && isDirectImport && (model.FieldType == ControlType.LabTreeType ||
|
||||
model.FieldType == ControlType.LabComboxValue ||
|
||||
model.FieldType == ControlType.LabComboxValueParam ||
|
||||
model.FieldType == ControlType.LabAutoCompleteValue ||
|
||||
model.FieldType == ControlType.LabAutoCompleteValueParam ||
|
||||
model.FieldType == ControlType.LabMultiSelectValue ||
|
||||
model.FieldType == ControlType.LabMultiSelectValueParam
|
||||
|| model.FieldType == ControlType.LabTreeLookValue
|
||||
))
|
||||
{
|
||||
bool isEmpty = model.CanNull;
|
||||
string fieldValue = GetValueByImportText(dataRow, col.FieldName, cell + "", isBillImport);
|
||||
if ((model.FieldType == ControlType.LabMultiSelectValue ||
|
||||
model.FieldType == ControlType.LabMultiSelectValueParam) && GetCellValue(cell).Contains(","))
|
||||
{
|
||||
string[] cellText = GetCellValue(cell).Split(',');
|
||||
foreach (string cellValue in cellText)
|
||||
{
|
||||
fieldValue += GetValueByImportText(dataRow, col.FieldName, cellValue, isBillImport) + ',';
|
||||
}
|
||||
fieldValue = fieldValue.TrimEnd(',');
|
||||
}
|
||||
if (string.IsNullOrEmpty(fieldValue))
|
||||
{
|
||||
if (isEmpty)
|
||||
{
|
||||
MessageUtil.Show("该列不允许为空->" + col.Caption + "");
|
||||
}
|
||||
else
|
||||
{
|
||||
GridColumn column = gridView.Columns[col.FieldName];
|
||||
if (column.ColumnType.Name.ToLower().IndexOf("int") >= 0)
|
||||
if (column.ColumnType.Name.ToLower() == "string")
|
||||
dataRow[col.FieldName] = "";
|
||||
if (column.ColumnType.Name.ToLower().IndexOf("char") >= 0)
|
||||
dataRow[col.FieldName] = "";
|
||||
if (column.ColumnType.Name.ToLower().IndexOf("int") >= 0)
|
||||
dataRow[col.FieldName] = 0;
|
||||
if (column.ColumnType.Name.ToLower() == "decimal")
|
||||
dataRow[col.FieldName] = 0;
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dataRow[col.FieldName] = fieldValue;
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
IRichTextString richTextString = (IRichTextString)cell.RichStringCellValue;
|
||||
if (fileExt == ".xlsx")
|
||||
{
|
||||
cell.SetCellValue(richTextString);
|
||||
}
|
||||
else if (fileExt == ".xls")
|
||||
{
|
||||
string cellValue = string.Empty;
|
||||
for (int m = 0; m < richTextString.Length; m++)
|
||||
{
|
||||
|
||||
//string aaa = richTextString.String.Substring(m , 1);
|
||||
//short aaa = richTextString.GetFontAtIndex(m);
|
||||
IFont font = sheet.Workbook.GetFontAt(richTextString.GetFontAtIndex(m));//richTextString.GetFontAtIndex(m)
|
||||
|
||||
string str = string.Empty;
|
||||
string code = richTextString.ToString().Substring(m, 1);
|
||||
switch (font.TypeOffset)
|
||||
{
|
||||
case FontSuperScript.Sub:
|
||||
str = ChemistryHelper.GetSubChar(code);
|
||||
break;
|
||||
|
||||
case FontSuperScript.Super:
|
||||
str = ChemistryHelper.GetSuperChar(code);
|
||||
break;
|
||||
default:
|
||||
str = code;
|
||||
break;
|
||||
}
|
||||
cellValue = cellValue + str;
|
||||
}
|
||||
cell.SetCellValue(cellValue);
|
||||
}
|
||||
dataRow[col.FieldName] = GetCellValue(cell);
|
||||
if (AutoImportCal)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(model.UnionFields))
|
||||
{
|
||||
SetUnionValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
SetCalcValue(model, dataRow[col.FieldName] + "", dataRow);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
table.Rows.Add(dataRow);
|
||||
count += 1;
|
||||
}
|
||||
ToAssignment = false;
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
|
||||
if (ToAssignment && (CurrentRow != 0 || CurrentColumn != 0))
|
||||
{
|
||||
string value = string.Format("第{0}行,第{1}列 {2},值出现异常", CurrentRow, CurrentColumn + 1, columnName);
|
||||
MessageUtil.Show(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageUtil.Show(ResourceKeys.DeleteLastTotalRow);
|
||||
}
|
||||
}
|
||||
catch (IOException exe)
|
||||
{
|
||||
//string Message = ErrorMessage.PromptErrorMessage(exe);
|
||||
//MessageUtil.Show(Message, exe.Message);
|
||||
MessageUtil.Show(string.Format(ResourceKeys.UseingFile, Path.GetFileName(fileName)));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//MessageUtil.Show(ResourceKeys.ImportFault + ex.Message);
|
||||
LogUtil.WriteError("读取excel出错!", ex);
|
||||
string Message = ErrorMessage.PromptErrorMessage(ex);
|
||||
MessageUtil.Show(Message, ex.Message);
|
||||
}
|
||||
return table.TrimEmptyRows();
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算列计算公式
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="fieldValue"></param>
|
||||
/// <param name="rowItem"></param>
|
||||
private void SetCalcValue(GridColumnModel model, string fieldValue, DataRow rowItem)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (rowItem != null && model != null)
|
||||
{
|
||||
foreach (GridColumn col in this.ParentView.Columns)
|
||||
{
|
||||
GridColumnModel colModel = col.Tag as GridColumnModel;
|
||||
if (colModel == null) continue;
|
||||
string defultValue = colModel.DefaultValue;
|
||||
if (colModel.FieldType == 7 && rowItem.Table.Columns.Contains(col.FieldName) && string.IsNullOrEmpty(rowItem[col.FieldName] + "") && !string.IsNullOrEmpty(colModel.DefaultValue))
|
||||
{
|
||||
defultValue = ReplaceHelper.ReplaceUserInfo(colModel.DefaultValue);
|
||||
if (defultValue.StartsWith("@"))
|
||||
{
|
||||
defultValue = BaseImpl.GetDefaultValue(ReplaceHelper.ReplaceRowParam(rowItem, defultValue));
|
||||
}
|
||||
|
||||
if (IsNumberic(defultValue)) rowItem[col.FieldName] = defultValue;//colModel.DefaultValue;
|
||||
|
||||
}
|
||||
}
|
||||
List<GridColumnModel> calcModels = ColumnList.FindAll(x => !string.IsNullOrEmpty(x.CalcExpr)).OrderBy(y => y.CalcOrder).ToList();
|
||||
foreach (GridColumnModel gridModel in calcModels)
|
||||
{
|
||||
// 计算顺序小于当前控件则不计算
|
||||
if (gridModel.CalcOrder < model.CalcOrder) continue;
|
||||
|
||||
if (!gridModel.FieldName.Equals(model.FieldName, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
bool isTruncate = false;
|
||||
string calcExpr = gridModel.CalcExpr;
|
||||
if (calcExpr.StartsWith("@truncate:"))
|
||||
{
|
||||
calcExpr = calcExpr.Substring(10);
|
||||
isTruncate = true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string result = string.Empty;
|
||||
if (calcExpr.StartsWith("@"))
|
||||
{
|
||||
result = BaseImpl.GetDefaultValue(ReplaceHelper.ReplaceRowParam(rowItem, calcExpr));
|
||||
}
|
||||
else
|
||||
{
|
||||
calcExpr = ReplaceHelper.ReplaceRowParam(rowItem, calcExpr);
|
||||
result = EvalHelper.Eval2(ReplaceHelper.ReplaceEvalCond(calcExpr)) + "";
|
||||
}
|
||||
if (isTruncate)
|
||||
{
|
||||
result += result.Contains(".") ? "0000000000" : ".0000000000";
|
||||
rowItem[gridModel.FieldName] = result.Substring(0, result.IndexOf(".")) + result.Substring(result.IndexOf("."), model.Decimals + 1);
|
||||
//this.gridView.SetRowCellValue(this.gridView.FocusedRowHandle, this.gridView.Columns[gridModel.FieldName], result.Substring(0, result.IndexOf(".")) + result.Substring(result.IndexOf("."), model.Decimals + 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
double d = 0;
|
||||
if (!string.IsNullOrEmpty(result) &&
|
||||
!"NaN".Equals(result, StringComparison.OrdinalIgnoreCase) &&
|
||||
!"非数字".Equals(result, StringComparison.OrdinalIgnoreCase) &&
|
||||
!"undefined".Equals(result, StringComparison.OrdinalIgnoreCase) &&
|
||||
result != "∞" && result != "-∞" &&
|
||||
!"正无穷大".Equals(result, StringComparison.OrdinalIgnoreCase) &&
|
||||
!"负无穷大".Equals(result, StringComparison.OrdinalIgnoreCase) &&
|
||||
double.TryParse(result, out d))
|
||||
{
|
||||
if (string.IsNullOrEmpty(gridModel.DataFormat))
|
||||
{
|
||||
result = Math.Round(Convert.ToDouble(d), model.Decimals, MidpointRounding.AwayFromZero) + "";
|
||||
}
|
||||
else
|
||||
{
|
||||
//计算后先执行保存小数位数
|
||||
if (SystemInfo.Instance.ComputeFormatting) result = string.Format("{0:" + gridModel.DataFormat + "}", Convert.ToDecimal(result));
|
||||
}
|
||||
if (model.FieldType == 7 && result.Contains('%'))
|
||||
{
|
||||
result = (double.Parse(result.Replace("%", "")) * 0.01).ToString();
|
||||
}
|
||||
rowItem[gridModel.FieldName] = result;
|
||||
// 判断计算列有无关联值
|
||||
GridColumnModel UnionModel = calcModels.FirstOrDefault(x => x.FieldName == gridModel.FieldName);
|
||||
if (!string.IsNullOrEmpty(UnionModel.UnionFields))
|
||||
{
|
||||
this.SetUnionValue(UnionModel, rowItem[gridModel.FieldName] + "", rowItem);
|
||||
}
|
||||
//this.gridView.SetRowCellValue(this.gridView.FocusedRowHandle, this.gridView.Columns[gridModel.FieldName], result);
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.WriteError(ex);
|
||||
LogUtil.WriteError("计算列计算公式--错误-->" + rowItem[gridModel.FieldName], ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断输入的字符串是否可以转换成数值类型
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNumberic(string str)
|
||||
{
|
||||
double vsNum;
|
||||
bool isNum;
|
||||
isNum = double.TryParse(str, System.Globalization.NumberStyles.Float,
|
||||
System.Globalization.NumberFormatInfo.InvariantInfo, out vsNum);
|
||||
return isNum;
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取数据源
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private DataTable GetSourceTable(DataTable dataTable)
|
||||
{
|
||||
DataTable sourceTable = new DataTable();
|
||||
sourceTable.Columns.Add("excelFieldName");
|
||||
sourceTable.Columns.Add("clientFieldName");
|
||||
if (dataTable != null)
|
||||
{
|
||||
foreach (DataColumn col in dataTable.Columns)
|
||||
{
|
||||
DataRow dataRow = sourceTable.NewRow();
|
||||
dataRow["excelFieldName"] = col.ColumnName;
|
||||
sourceTable.Rows.Add(dataRow);
|
||||
}
|
||||
}
|
||||
return sourceTable;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user