Loading

导出Execl

public static void ExportExcelFile(DevExpress.Xpf.Grid.TableView tableView)
        {
            if (tableView == null)
            {
                MessageBox.Show("导出失败!", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = "c:\\";
            saveFileDialog.Filter = "Excel(*.xls)|*.xls|Excel2007(*.xlsx)|*.xlsx";
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FilterIndex = 1;
            bool? result = saveFileDialog.ShowDialog();
            if (result == true)
            {
                string filename = saveFileDialog.FileName;
                int index = filename.IndexOf(".");
                string suffix = filename.Substring(index + 1);
                if (suffix == "xls")
                {
                    tableView.ExportToXls(filename);
                }
                else
                {
                    tableView.ExportToXlsx(filename);
                }

                MessageBox.Show("导出成功!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        public static void ExportExcelFile(DataTable dt)
        {
            if (dt == null) return;

            
            //gridControl.View.ExportToXlsx()

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = "c:\\";
            saveFileDialog.Filter = "Excel(*.xls)|*.xls|Excel2007(*.xlsx)|*.xlsx";
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.FilterIndex = 1;
            bool? result = saveFileDialog.ShowDialog();
            if (result == true)
            {
                

                string filename = saveFileDialog.FileName;
                //int index = filename.IndexOf(".");
                //string suffix = filename.Substring(index + 1);

                Workbook workbook = new Workbook();
                //清除页先 要不然 新建就有一个sheet
                workbook.Worksheets.Clear();
                workbook.Worksheets.Add(dt.TableName);
                Worksheet sheet = workbook.Worksheets[0];
                Cells cells = sheet.Cells;  //单元格

                int colnum = dt.Columns.Count;      //表格列数
                int rownum = dt.Rows.Count;         //表格行数

                //for (int x = 0; x < colnum; x++)
                //{
                //    cells[0, x].PutValue(dt.Columns[x].ColumnName);
                //}
                for (int x = 0; x < rownum; x++)
                {
                    for (int y = 0; y < colnum; y++)
                    {
                        if (x == 0)
                            cells[x, y].PutValue(dt.Columns[y].ColumnName);
                        
                        cells[x + 1, y].PutValue(dt.Rows[x][y].TrimOrNull());
                    }
                }

                SetColumnWithAuto(sheet);
                workbook.Save(filename);

                MessageBox.Show("导出成功!", "信息提示", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            
            dt.Clear();
            dt.Dispose();
            dt = null;
        }

  

posted @ 2021-07-07 14:39  jevan  阅读(49)  评论(0编辑  收藏  举报