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;
                }
            }
        }
        
        

Guess you like

Origin blog.csdn.net/u012842630/article/details/118344995