zoukankan      html  css  js  c++  java
  • 导出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;
            }
    

      

  • 相关阅读:
    Git SSH Key 生成步骤
    IOS音频1:之采用四种方式播放音频文件(一)AudioToolbox AVFoundation OpenAL AUDIO QUEUE
    IOS网络篇1之截取本地URL请求(NSURLProtocol)
    IOS 视频直播/智能家居(一行行敲代码,从零开始)lesson:1整体架构
    iOS应用支持IPV6,就那点事儿
    App store最新审核标准公布
    iOS应用内付费(IAP)开发步骤列表
    iOS应用内支付(IAP)的那些坑
    IOS 第三方支付的使用:支付宝
    亲们,委托你们是否已忘记
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/14981402.html
Copyright © 2011-2022 走看看