Esempio n. 1
0
        private TCell GetPreCell(TSheet sheet, TCell cell)
        {
            var row = sheet.Head.Rows[cell.RowIndex];

            for (int i = 0; i < row.Cells.Count; i++)
            {
                if (cell == row.Cells[i])
                {
                    return(row.Cells[i - 1]);
                }
            }
            return(null);
        }
Esempio n. 2
0
        public void Load(string filePath)
        {
            this.filePath = filePath;
            XmlDocument doc = new XmlDocument();

            doc.Load(this.filePath);
            XmlNodeList nodeList = doc.SelectNodes("dataSet/table");

            foreach (XmlNode node in nodeList)
            {
                TSheet sheet = CreateSheet(node);
                template.Sheets.Add(sheet);
            }
        }
Esempio n. 3
0
 private void CalculateCells(TSheet sheet)
 {
     // 跨列
     for (int i = 0; i < sheet.Head.Rows.Count; i++)
     {
         var row = sheet.Head.Rows[i];
         for (int j = 0; j < row.Cells.Count; j++)
         {
             var cell = row.Cells[j];
             if (cell.ColSpan > 1)  //跨列
             {
                 for (int m = j + 1; m < row.Cells.Count; m++)
                 {
                     row.Cells[m].ColumnIndex += cell.ColSpan - 1;
                 }
             }
         }
     }
     // 跨行
     for (int i = 0; i < sheet.Head.Rows.Count; i++)
     {
         var row = sheet.Head.Rows[i];
         for (int j = 0; j < row.Cells.Count; j++)
         {
             var cell = row.Cells[j];
             if (cell.RowSpan < 2)
             {
                 continue;
             }
             // 如果跨行,计算单元格跨列
             // 循环计算下一行的单元格跨列
             for (int ii = i + 1; ii < sheet.Head.Rows.Count; ii++)
             {
                 var row2 = sheet.Head.Rows[ii];
                 for (int jj = 0; jj < row2.Cells.Count; jj++)
                 {
                     var cell2 = row2.Cells[jj];
                     if (cell2.ColumnIndex >= cell.ColumnIndex)
                     {
                         cell2.ColumnIndex += cell.ColSpan;
                     }
                 }
             }
         }
     }
 }
Esempio n. 4
0
        private TSheet CreateSheet(XmlNode tableNode)
        {
            TSheet sheet = new TSheet();

            sheet.Head = new THead();
            if (tableNode.Attributes["name"] != null)
            {
                sheet.Name = tableNode.Attributes["name"].Value;
            }
            XmlNodeList nodeList = tableNode.SelectNodes("thead/tr");

            for (int i = 0; i < nodeList.Count; i++)
            {
                XmlNode tr  = nodeList[i];
                TRow    row = new TRow();
                for (int j = 0; j < tr.ChildNodes.Count; j++)
                {
                    XmlNode th   = tr.ChildNodes[j];
                    TCell   cell = new TCell();
                    cell.RowIndex = i;
                    cell.Name     = th.Attributes["field"] == null ? string.Empty : th.Attributes["field"].Value;
                    cell.Caption  = th.InnerText;
                    if (th.Attributes["colspan"] != null)
                    {
                        cell.ColSpan = int.Parse(th.Attributes["colspan"].Value);
                    }
                    if (th.Attributes["rowspan"] != null)
                    {
                        cell.RowSpan = int.Parse(th.Attributes["rowspan"].Value);
                    }
                    cell.ColumnIndex = j;
                    row.Cells.Add(cell);
                }
                sheet.Head.Rows.Add(row);
            }
            CalculateCells(sheet);
            return(sheet);
        }
Esempio n. 5
0
 public void Load(DataSet ds)
 {
     foreach (DataTable dt in ds.Tables)
     {
         TSheet sheet = new TSheet();
         sheet.Title = dt.TableName;
         sheet.Name  = sheet.Title;
         sheet.Head  = new THead();
         TRow row = new TRow();
         sheet.Head.Rows.Add(row);
         int i = 0;
         foreach (DataColumn column in dt.Columns)
         {
             TCell cell = new TCell();
             cell.RowIndex    = 0;
             cell.ColumnIndex = i;
             cell.Name        = column.ColumnName;
             cell.Caption     = string.IsNullOrEmpty(column.Caption)?column.ColumnName:column.Caption;
             row.Cells.Add(cell);
             i++;
         }
         this.template.Sheets.Add(sheet);
     }
 }
Esempio n. 6
0
 private void calculate(TSheet tSheet)
 {
 }
Esempio n. 7
0
        private void fillSheet(ISheet sheet, TSheet tSheet, DataTable dt)
        {
            calculate(tSheet);
            // fill header
            int          rownum = 0;
            List <TCell> tCells = new List <TCell>();

            foreach (TRow tRow in tSheet.Head.Rows)
            {
                IRow row = sheet.CreateRow(rownum);
                rownum++;
                foreach (TCell tCell in tRow.Cells)
                {
                    tCells.Add(tCell);
                    ICell cell = row.CreateCell(tCell.ColumnIndex);
                    cell.SetCellValue(tCell.Caption);
                }
            }
            // create region
            creatCellRanges(sheet, tCells);
            // get columns
            int          m          = 0;
            List <TCell> realTCells = new List <TCell>();

            while (true)
            {
                bool find = false;
                foreach (var tCell in tCells)
                {
                    if (tCell.ColumnIndex == m)
                    {
                        find = true;
                        if (realTCells.Count == m + 1)
                        {
                            if (realTCells[m].RowIndex < tCell.RowIndex)
                            {
                                realTCells[m] = tCell;
                            }
                        }
                        else
                        {
                            realTCells.Add(tCell);
                        }
                    }
                }
                m++;
                if (!find)
                {
                    break;
                }
            }
            // fill sheet
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr  = dt.Rows[i];
                IRow    row = sheet.CreateRow(rownum + i);
                foreach (TCell tCell in realTCells)
                {
                    ICell  cell = row.CreateCell(tCell.ColumnIndex);
                    object o    = dr[tCell.Name];
                    cell.SetCellValue(o.ToString());
                }
            }
            // set column
            setColumnWidth(sheet, tCells);
        }
Esempio n. 8
0
        private int GetColumnIndex(TSheet sheet, TCell cell)
        {
            TCell preCell = GetPreCell(sheet, cell);

            return(0);
        }