//============================================================ protected string FormatValue(FXlsColumn column, string value) { string type = column.Type; // 如果是TUint32类型的将字符串中的“-”去掉 if ("TUint32" == column.Type) { if (value.StartsWith("-")) { value = value.Replace("-", ""); value = "-" + value; } else { value = value.Replace("-", ""); } } // 去掉字符串中的空格 value = value.Trim(); return(value); }
//============================================================ protected bool LoadFileRows(Excel.Worksheet worksheet) { // 检查列表 if (_columns.IsEmpty) { return(false); } Excel.Range cells = worksheet.Cells; // 读取头数据 int columnIndex = 0; string code = _columns.Value(0).Name; string id = _columns[0].Name; for (int n = 1; n < DATA_COL_MAXCNT; n++) { string name = cells[DATA_NAME_ROW, n].Text; if (RString.IsEmpty(name)) { break; } if (_columns.Contains(name)) { FXlsColumn column = _columns.Get(name); column.Index = n; if (name == code) { columnIndex = n; } } } // 计算最大行数和列数 int count = _columns.Count; Range rowRange = worksheet.Rows[1]; object[,] rowValues = rowRange.get_Value(); int columnCount = 1; for (; columnCount < DATA_COL_MAXCNT; columnCount++) { if (rowValues[1, columnCount] == null) { break;; } } string columnCode = ""; int ch = columnCount / 26; int cl = columnCount % 26; if (ch > 0) { columnCode += (char)('A' + ch - 1); } columnCode += (char)('A' + cl); Range columnRange = worksheet.Columns[columnIndex]; object[,] columnValues = columnRange.get_Value(); int rowCount = 1; for (; rowCount < DATA_ROW_MAXCNT; rowCount++) { if (columnValues[rowCount, 1] == null) { break;; } } // 获得数据 Range range = worksheet.get_Range("A3:" + columnCode + rowCount); object[,] values = range.get_Value(); // 转换数据 for (int r = 1; r <= rowCount; r++) { FXlsRow row = new FXlsRow(); for (int n = 0; n < count; n++) { FXlsColumn column = _columns.Value(n); int index = column.Index; if (index > 0) { object value = values[r, index]; string valueStr = String.Empty; if (null != value) { valueStr = value.ToString(); if (!RString.IsEmpty(valueStr)) { valueStr = FormatValue(column, valueStr); } } row.Set(column.Name, valueStr); } } //for(int n = 0; n < count; n++) { // FXlsColumn column = _columns.Value(n); // int index = column.Index; // if(index > 0) { // string value = cells[r, index].Text; // if(!RString.IsEmpty(value)) { // value = FormatValue(column, value); // } // row.Set(column.Name, value); // } //} if (!row.Contains(code)) { break; } if (RString.IsEmpty(row.Get(code))) { break; } _rows.Push(row); } return(true); }
//============================================================ protected void LoadFileColumns(Excel.Worksheet worksheet) { // 读取定义 int count = 1; // 列数 int columnsNum = 0; // 行数 int rowsNum = 0; // id所在列数 int idcolumn = 0; // group所在列数 int groupcolumn = 0; // label所在列数 int labelcolumn = 0; // name所在列数 int namecolumn = 0; // type所在列数 int typecolumn = 0; // dataType所在列数 int datacolumn = 0; // translate所在列数 int translatecolumn = 0; // length所在列数 int lengthcolumn = 0; // total所在列数 int totalcolumn = 0; // note所在列数 int notecolumn = 0; Excel.Range cells = worksheet.Cells; string cellText = cells[DATA_NAME_ROW, count].Text; // 找到各属性所在列 while ("" != cellText) { if ("id" == cellText) { idcolumn = count; } if ("group" == cellText) { groupcolumn = count; } if ("label" == cellText) { labelcolumn = count; } if ("name" == cellText) { namecolumn = count; } if ("type" == cellText) { typecolumn = count; } if ("data_type" == cellText) { datacolumn = count; } if ("translate" == cellText) { translatecolumn = count; } if ("length" == cellText) { lengthcolumn = count; } if ("total" == cellText) { totalcolumn = count; } if ("note" == cellText) { notecolumn = count; } count++; cellText = cells[DATA_NAME_ROW, count].Text; } count--; columnsNum = count; count = 3; if (0 == idcolumn) { return; } // 获得行数 cellText = cells[count, idcolumn].Text; while ("" != cellText) { count++; cellText = cells[count, idcolumn].Text; } count--; rowsNum = count; // 获取数据 for (int i = 3; i <= rowsNum; i++) { FXlsColumn column = new FXlsColumn(); cellText = cells[i, groupcolumn].Text; if ("" != cellText) { column.Group = cellText; } cellText = cells[i, idcolumn].Text; if ("" != cellText) { column.Id = cellText; } cellText = cells[i, labelcolumn].Text; if ("" != cellText) { column.Label = cellText; } cellText = cells[i, namecolumn].Text; if ("" != cellText) { column.Name = cellText; } cellText = cells[i, typecolumn].Text; if ("" != cellText) { column.Type = cellText; } if (datacolumn != 0) { cellText = cells[i, datacolumn].Text; if ("" != cellText) { column.DataType = cellText; } } cellText = cells[i, translatecolumn].Text; if ("" != cellText) { column.Translate = cellText; } cellText = cells[i, lengthcolumn].Text; if ("" != cellText) { column.Lenth = cellText; } cellText = cells[i, totalcolumn].Text; if ("" != cellText) { column.Total = cellText; } cellText = cells[i, notecolumn].Text; if ("" != cellText) { column.Note = cellText; } // 判断是否无数据 if (null != column.Name) { _columns.Set(column.Name, column); } } }