Example #1
0
 private void Clear()
 {
     m_ColumnCount = 0;
     m_RowCount    = 0;
     m_CsvPackage  = null;
     m_WorkSheet   = null;
     m_SheetReader = null;
     m_KeyToRowDic.Clear();
 }
Example #2
0
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                string voName = typeof(T).Name;
                if (EditorGamingMapData.m_VoNameDic != null && !EditorGamingMapData.m_VoNameDic.ContainsKey(voName))
                {
                    return;
                }
                List <string> fileNameList = EditorGamingMapData.m_VoNameDic[voName];
                if (fileNameList == null || fileNameList.Count != 2)
                {
                    return;
                }
                //ESheetReader reader = EditorConfigData.GetXlsxFileSheetReader(fileNameList[0], fileNameList[1]);
                ESheetReader reader = EditorConfigData.GetCSVFileSheetReader(fileNameList[0], fileNameList[1]);
                if (reader == null)
                {
                    return;
                }

                voDir    = new Dictionary <int, T>();
                voList   = new List <T>();
                voStrDir = new Dictionary <string, T>();
                T tmp;
                for (int i = ACTUALDATA_ROW - 1; i < reader.GetRowCount(); i++)
                {
                    tmp = Activator.CreateInstance <T>();
                    tmp.CopyFrom(i, reader);

                    if (!string.IsNullOrEmpty(tmp.strID))
                    {
                        if (!voStrDir.ContainsKey(tmp.strID))
                        {
                            voStrDir.Add(tmp.strID, tmp);
                            voList.Add(tmp);
                        }
                    }
                    else
                    {
                        if (!voDir.ContainsKey(tmp.ID) && tmp.ID > 0)
                        {
                            voDir.Add(tmp.ID, tmp);
                            voList.Add(tmp);
                        }
                    }
                }
            }
            catch (Exception e)
            {
            }
        }
Example #3
0
 /// <summary>
 /// 设置当前行和reader
 /// </summary>
 /// <param name="row"></param>
 /// <param name="reader"></param>
 public virtual void CopyFrom(int row, ESheetReader reader)
 {
     m_CurRow    = row;
     m_CurReader = reader;
 }
Example #4
0
    public void OpenCSV(string csvPath, string sheetName)
    {
        Clear();
        string[][]      resultdata = null;
        ExcelTextFormat format     = new ExcelTextFormat();

        format.Delimiter     = ',';
        format.EOL           = "\n";
        format.TextQualifier = '"';
        format.Encoding      = new UTF8Encoding();

        string fileData = string.Empty;

        using (StreamReader reader = new StreamReader(csvPath, Encoding.UTF8))
        {
            fileData = reader.ReadToEnd();
        }

        fileData = fileData.Replace("\r", "");

        try
        {
            using (m_CsvPackage = new ExcelPackage())
            {
                ExcelWorkbook workbook = m_CsvPackage.Workbook;
                m_WorkSheet = workbook.Worksheets.Add(sheetName);
                var range = m_WorkSheet.Cells["A1"].LoadFromText(fileData, format);
                if (workbook.Worksheets.Count > 0)
                {
                    int columnMin = m_WorkSheet.Dimension.Start.Column;
                    int rowMin    = m_WorkSheet.Dimension.Start.Row;

                    m_ColumnCount = m_WorkSheet.Dimension.End.Column;
                    m_RowCount    = m_WorkSheet.Dimension.End.Row;
                    resultdata    = new string[m_RowCount][];

                    for (int i = rowMin; i <= m_RowCount; i++)
                    {
                        bool Arow = true;
                        int  trow = i - 1;
                        resultdata[i - 1] = new string[m_ColumnCount];
                        for (int j = columnMin; j <= m_ColumnCount; j++)
                        {
                            ExcelRange data = m_WorkSheet.Cells[i, j];
                            if (data != null && data.Value != null)
                            {
                                resultdata[trow][j - 1] = data.Value.ToString();
                                if (Arow)
                                {
                                    if (!m_KeyToRowDic.ContainsKey(data.Value.ToString()))
                                    {
                                        m_KeyToRowDic.Add(data.Value.ToString(), trow);
                                    }
                                    Arow = false;
                                }
                            }
                        }
                    }
                    m_SheetReader = new ESheetReader(resultdata);
                }
            }
        }
        catch (System.Exception ex)
        {
            Debug.LogError(ex.StackTrace);
        }
    }