Esempio n. 1
0
        protected virtual IWorkbook CreateExportSheet(int rowCount, ExcelExtension ext)
        {
            return(this.UIDispatcher.Invoke(new Func <IWorkbook>(() =>
            {
                IWorkbook workbook = ext == ExcelExtension.XLS ? (IWorkbook) new HSSFWorkbook() : new XSSFWorkbook();
                ISheet sheet = workbook.CreateSheet(this.Header);
                var firstRow = sheet.CreateRow(0);

                var bindPropCount = this.Table.Columns.Count() - this.Table.Columns.Where(c => string.IsNullOrWhiteSpace(GridColumn.GetBindProp(c))).Count();
                for (int i = 0; i < bindPropCount; i++)
                {
                    DataGridColumn col = this.Table.Columns[i];
                    firstRow.CreateCell(i, CellType.String).SetCellValue(col.Header.ToString());
                }

                Array context = this.GetExportData(rowCount);
                for (int i = 0; i < context.Length; i++)
                {
                    IRow row = sheet.CreateRow(i + 1);
                    object rowData = context.GetValue(i);
                    for (int j = 0; j < bindPropCount; j++)
                    {
                        DataGridColumn col = this.Table.Columns[j];
                        string bindProp = GridColumn.GetBindProp(col);
                        IValueConverter cvt = GridColumn.GetConverter(col);
                        object cellValue = rowData.GetType().GetProperty(bindProp).GetValue(rowData, null);
                        if (cvt != null)
                        {
                            cellValue = cvt.Convert(cellValue, typeof(string), null, null);
                        }
                        row.CreateCell(j, CellType.String).SetCellValue((cellValue ?? string.Empty).ToString());
                    }
                }
                return workbook;
            }), null) as IWorkbook);
        }