コード例 #1
0
        /// <summary>
        /// 使用背景线程导出Excel文档
        /// </summary>
        private void bg_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable table = new DataTable();

            if (AllToExport != null && IsExportAllPage)
            {
                if (AllToExport is DataView)
                {
                    DataView dv = (DataView)AllToExport;//默认导出显示内容
                    table = dv.ToTable();
                }
                else if (AllToExport is DataTable)
                {
                    table = AllToExport as DataTable;
                }
                else
                {
                    table = ReflectionUtil.CreateTable(AllToExport);
                }

                //解析标题
                string originalName = string.Empty;
                foreach (DataColumn column in table.Columns)
                {
                    originalName = column.Caption;
                    if (columnNameAlias.ContainsKey(originalName.ToUpper()))
                    {
                        var caption = columnNameAlias[originalName.ToUpper()];
                        //多语言处理
                        caption = JsonLanguage.Default.GetString(caption);

                        column.Caption    = caption;
                        column.ColumnName = columnNameAlias[originalName.ToUpper()];
                    }
                }
                //for (int i = 0; i < this.gridView1.Columns.Count; i++)
                //{
                //    if (!this.gridView1.Columns[i].Visible)
                //    {
                //        table.Columns.Remove(this.gridView1.Columns[i].FieldName);
                //    }
                //}
            }
            else
            {
                DataColumn column;
                DataRow    row;
                for (int i = 0; i < this.gridView1.Columns.Count; i++)
                {
                    if (this.gridView1.Columns[i].Visible)
                    {
                        column         = new DataColumn(this.gridView1.Columns[i].FieldName, typeof(string));
                        column.Caption = this.gridView1.Columns[i].Caption;
                        table.Columns.Add(column);
                    }
                }

                object cellValue = "";
                string fieldName = "";
                for (int i = 0; i < gridView1.RowCount; i++)
                {
                    row = table.NewRow();
                    for (int j = 0; j < gridView1.Columns.Count; j++)
                    {
                        if (this.gridView1.Columns[j].Visible)
                        {
                            fieldName      = gridView1.Columns[j].FieldName;
                            cellValue      = gridView1.GetRowCellValue(i, fieldName);
                            row[fieldName] = cellValue ?? "";
                        }
                    }
                    table.Rows.Add(row);
                }
            }

            string outError = "";

            AsposeExcelTools.DataTableToExcel2(table, (String)e.Argument, out outError);
        }