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

      

  • 相关阅读:
    【leetcode】1228.Missing Number In Arithmetic Progression
    【leetcode】1227. Airplane Seat Assignment Probability
    【leetcode】1224. Maximum Equal Frequency
    【leetcode】1222. Queens That Can Attack the King
    【leetcode】1221. Split a String in Balanced Strings
    【leetcode】1219. Path with Maximum Gold
    【leetcode】1220. Count Vowels Permutation
    【leetcode】1218. Longest Arithmetic Subsequence of Given Difference
    【leetcode】1217. Play with Chips
    2018.11.27 元器件选型(2)- 连接器
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/14981402.html
Copyright © 2020-2023  润新知