BlackLanzer / excellibrary

Automatically exported from code.google.com/p/excellibrary
0 stars 0 forks source link

test for FormatTypes in DatasetTable.PopulateTable #9

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
We should analyse the second row to find the correct type and use that type 
for the DataColumn.. Currently i only changed to add the DateTimeValue 
instead of the Value.

for (int currentRowIndex = 1; currentRowIndex <= Cells.LastRowIndex; 
currentRowIndex++)
            {
                DataRow dr = dt.NewRow();
                object value = null;
                for (int currentColumnIndex = 0; currentColumnIndex <= 
Cells.LastColIndex; currentColumnIndex++) {
                  if (Cells[currentRowIndex, currentColumnIndex].IsEmpty)
                    dr[currentColumnIndex] = Cells[currentRowIndex, 
currentColumnIndex];
                  else {
                    switch (Cells[currentRowIndex, 
currentColumnIndex].Format.FormatType)
                    {
                      case CellFormatType.Date:
                      case CellFormatType.Time:
                      case CellFormatType.DateTime:
                        value = Cells[currentRowIndex, 
currentColumnIndex].DateTimeValue;
                          break;
                      default:
                        value = Cells[currentRowIndex, 
currentColumnIndex].Value;
                        break;
                    }
                    dr[currentColumnIndex] = value;
                  }
                }
                dt.Rows.Add(dr);
            }

Original issue reported on code.google.com by marco.di...@gmail.com on 11 Feb 2009 at 10:01

GoogleCodeExporter commented 8 years ago

Original comment by jetcat on 12 Feb 2009 at 3:31

GoogleCodeExporter commented 8 years ago

Original comment by jetcat on 12 Feb 2009 at 3:32

GoogleCodeExporter commented 8 years ago
Workbook book = Workbook.Load(filename);
                Worksheet sheet = book.Worksheets[0];
                if (sheet.Cells.Rows.Count > 0)
                {
                    DataTable tab = new DataTable();
                    Row HeadRow = sheet.Cells.GetRow(0);
                    DataColumn dc;

                    //把第一行当成列标题  the First Row is ColumName
                    for (int a = 0; a < HeadRow.LastColIndex; a++)
                    {
                        dc = new DataColumn(HeadRow.GetCell(a).StringValue);
                        tab.Columns.Add(dc);
                    }
                    for (int rowIndex = sheet.Cells.FirstRowIndex + 1; rowIndex <= sheet.Cells.LastRowIndex; rowIndex++)
                    {
                        DataRow dr = tab.NewRow();
                        Row row = sheet.Cells.GetRow(rowIndex);
                        for (int colIndex = row.FirstColIndex; colIndex <= row.LastColIndex; colIndex++)
                        {
                            //Can not use row.GetCell(colIndex).Format.FormatType 
                            if (HeadRow.GetCell(colIndex).StringValue.IndexOf("日期") >= 0)
                            {
                                Double df = double.Parse(row.GetCell(colIndex).StringValue);
                                DateTime dt= DateTime.FromOADate(df);
                                dr[colIndex] = dt.ToString("yyyy-MM-dd HH:mm:ss");
                            }

                            else { dr[colIndex] = row.GetCell(colIndex).StringValue; }
                        }
                        tab.Rows.Add(dr);
                    }
                    book = null;
                    sheet = null;
                    dgv.DataSource = tab;

Original comment by mingiqu...@gmail.com on 15 Nov 2013 at 8:43