c#快速导出csv或excel


        private void btn_Export_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.FileName = DateTime.Now.ToString("yyyy-MM-dd");
            sfd.Filter = "(*.xls)|*.xls|(*.xlsx)|*.xlsx|(*.csv)|*.csv";
            
            sfd.FilterIndex = 1;//首选xlsx
            if(dt.Rows.Count>500000)
                sfd.FilterIndex = 2;//大于500
                
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                if (sfd.FileName.EndsWith(".csv"))
                    SaveToCsv(dt, sfd.FileName);
                else
                    WriteToExcel(dt, sfd.FileName, "Sheet1");
                MessageBox.Show("Save OK");
            }
        }

        void SaveToCsv(DataTable dt, string path)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                sb.Append(dt.Columns[i].ColumnName.Replace(" ", "") + ",");
            }
            sb.AppendLine();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    sb.Append(dt.Rows[i][j].ToString() + ",");
                }
                sb.AppendLine();
                if (i % 1000 == 0)
                {
                    File.AppendAllText(path, sb.ToString());
                    sb = new StringBuilder();
                }
            }
            File.AppendAllText(path, sb.ToString());
        }
        
        
        public static void WriteToExcel(DataTable dt, string filePathAndName, string sheetname)
        {
            if (!string.IsNullOrEmpty(filePathAndName) && null != dt && dt.Rows.Count > 0)
            {
                IWorkbook book = new HSSFWorkbook();
                if(filePathAndName.EndsWith(".xlsx"))
                {
                    book = new XSSFWorkbook();
                }
                ISheet sheet = book.CreateSheet(sheetname);
                ICellStyle styleone = book.CreateCellStyle();
                IFont font1 = book.CreateFont();
                font1.Color = 18;//颜色:绿色
                font1.FontHeightInPoints = 12;
                styleone.SetFont(font1);
                HSSFColor hssfcolor = new HSSFColor();
                styleone.FillBackgroundColor = 22;
                for (int i = 0; i < dt.Columns.Count; i++)
                    sheet.SetColumnWidth(i, 25 * 256);                       //设置列宽


                IRow row = sheet.CreateRow(0);
                row.Height = 100 * 5;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName); //列名
                    sheet.GetRow(0).GetCell(i).CellStyle = styleone;
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    IRow row2 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
                    }
                }

                //列宽自适应,只对英文和数字有效
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sheet.AutoSizeColumn(i);
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    using (FileStream fs = new FileStream(filePathAndName, FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                    }
                    book = null;
                }
            }
        }
        
        

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@David Liu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值