Exemplo n.º 1
0
        public void SetSource(ExportTable source)
        {
            Workbook  book  = new Workbook();
            Worksheet sheet = book.Worksheets[0];

            foreach (ExportField field in source.Columns)
            {
                Cell wcell = sheet.Cells[0, field.ColumnIndex];
                wcell.PutValue(field.DisplayText);
            }

            foreach (ExportRow row in source.Rows)
            {
                foreach (ExportField field in source.Columns)
                {
                    Cell       wcell = sheet.Cells[row.Index + 1, field.ColumnIndex];
                    ExportCell ecell = row.Cells[field.ColumnIndex];
                    string     value = ecell.Value;

                    IConverter converter = ConverterFactory.GetInstance(field.Converter);
                    value = converter.Convert(value);

                    IDataType dataType = DataTypeFactory.GetInstance(field.DataType);
                    dataType.SetValue(value);
                    if (!dataType.IsValidDataType)
                    {
                        wcell.PutValue(value, false);
                    }
                    else
                    {
                        switch (field.DataType.ToLower())
                        {
                        case "integer":
                            //wcell.PutValue(int.Parse(value));
                            wcell.PutValue(value, true);
                            break;

                        case "double":
                            //wcell.PutValue(double.Parse(value));
                            wcell.PutValue(value, true);
                            break;

                        case "datetime":
                            //int styleIndex = book.Styles.Add();
                            //Style style = book.Styles[styleIndex];
                            //style.Number = 14;
                            //style.Custom = "yyyy/MM/dd;@";
                            //wcell.Style.Copy(style);
                            DateTime dt = Convert.ToDateTime(dataType.GetTypeValue());
                            wcell.PutValue(dt.ToShortDateString(), false);
                            //wcell.PutValue(DateTime.Parse(value));
                            break;

                        case "object":
                            wcell.PutValue((object)value);
                            break;

                        default:
                            wcell.PutValue(value, false);
                            break;
                        }
                    }
                }
            }
            _book = book;
        }