Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test for FormatTypes in DatasetTable.PopulateTable #9

Open
GoogleCodeExporter opened this issue Dec 17, 2015 · 3 comments
Open

test for FormatTypes in DatasetTable.PopulateTable #9

GoogleCodeExporter opened this issue Dec 17, 2015 · 3 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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

  • Changed state: Accepted
  • Added labels: Type-Enhancement
  • Removed labels: Type-Defect

@GoogleCodeExporter
Copy link
Author

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

@GoogleCodeExporter
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant