• 导出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;
            }
    

      

  • 相关阅读:
    BeautifulSoup的基本用法
    [leedcode 189] Rotate Array
    [leedcode 187] Repeated DNA Sequences
    [leedcode 179] Largest Number
    [leedcode 174] Dungeon Game
    [leedcode 173] Binary Search Tree Iterator
    [leedcode 172] Factorial Trailing Zeroes
    [leedcode 171] Excel Sheet Column Number
    [leedcode 169] Majority Element
    [leedcode 168] Excel Sheet Column Title
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/14981402.html
Copyright © 2020-2023  润新知