Ejemplo n.º 1
0
 public CTableTemplate(Worksheet sheet)
 {
     RangeBound        = new CCell(1, 1);
     DataContent       = new Dictionary <int, CRow>();
     m_UsedDataContent = sheet.UsedRange.Value;
     _Init();
 }
Ejemplo n.º 2
0
        public bool ExportTable(string path)
        {
            if (m_bIsExporting)
            {
                Clog.Instance.LogError("请等待上一次导出完成");
                return(false);
            }
            m_bIsExporting = true;
            if (!CExcelManager.Instance.Open(path))
            {
                m_bIsExporting = false;
                Clog.Instance.LogError("Excel打开失败");
                return(false);
            }
            Sheets sheets = CExcelManager.Instance.GetSheets();

            if (sheets == null)
            {
                m_bIsExporting = false;
                Clog.Instance.LogError("Excel找不到有效页签供导出");
                return(false);
            }
            int sheetsCount = sheets.Count;

            for (int i = 2; i <= sheetsCount; i++)
            {
                Worksheet sheet = sheets[i];
                if (sheet.Name.Contains("~"))
                {
                    continue;
                }
                string exportPath = CCommon.GetValue(CCommon.key_tableEP);
                if (!CFileManager.DirectorExist(exportPath))
                {
                    Clog.Instance.LogError("导出路径不存在:" + exportPath);
                    continue;
                }
                CTableTemplate tableTemplate = new CTableTemplate(sheet);
                if (tableTemplate == null)
                {
                    Clog.Instance.LogError(sheet.Name + "导出失败");
                    continue;
                }
                string        excelFilePath = Path.Combine(exportPath, sheet.Name + ".txt");
                FileStream    fs            = CFileManager.Open(excelFilePath, FileMode.Create);
                StreamWriter  st            = new StreamWriter(fs);
                StringBuilder s             = new StringBuilder();
                CCell         sheetBound    = tableTemplate.RangeBound;

                string strEnd = _GetColumnChar(sheetBound.Column) + sheetBound.Row;
                Range  cells  = sheet.Range["A1", strEnd].Cells;
                object[,] realCells = cells.Value;
                for (int r = 1; r <= sheetBound.Row; r++)
                {
                    s.Clear();
                    string cellValue = string.Empty;
                    object o         = realCells[r, 1];
                    if (o != null)
                    {
                        cellValue = o.ToString();
                    }
                    if (cellValue == CCommon.StrSkipRows)
                    {
                        continue;
                    }
                    for (int c = 1; c <= sheetBound.Column; c++)
                    {
                        cellValue = string.Empty;
                        o         = realCells[r, c];
                        if (o != null)
                        {
                            cellValue = o.ToString();
                        }
                        s.Append(cellValue);
                        s.Append(",");
                    }
                    st.WriteLine(s.ToString());
                }
                st.Close();
                fs.Close();
                st.Dispose();
                fs.Dispose();
                Clog.Instance.Log(sheet.Name + "导出成功");
            }
            m_bIsExporting = false;
            return(true);
        }