| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.IO;using System.Windows.Forms;using DevComponents.DotNetBar.SuperGrid;namespace WCS_Client.UC{    public class ExportUtil    {        public static string ShowSaveFileDialog()        {            try            {                //对话框                SaveFileDialog Sfd = new SaveFileDialog();                Sfd.Title = "导出数据";                Sfd.Filter = "EXECL文件(*.xls) |*.xls|EXECL文件(*.xlsx) |*.xlsx|文本文件(*.txt)|*.txt";                Sfd.FilterIndex = 0;                if (Sfd.ShowDialog() == DialogResult.OK)                {                    return Sfd.FileName;                }            }            catch            {            }            return "";        }        public static void Execute(string PathFileName, DataTable dt, List<GridColumn> gcList)        {            try            {                if (gcList == null || gcList.Count <= 0)                {                    return;                }                if (dt == null)                {                    dt = new DataTable();                }                if (!string.IsNullOrEmpty(PathFileName))                {                    FileInfo fi = new FileInfo(PathFileName);                    if (!Directory.Exists(fi.DirectoryName))                    {                        Directory.CreateDirectory(fi.DirectoryName);                    }                    if (File.Exists(PathFileName))                    {                        File.Delete(PathFileName);                    }                    FileStream fs = new FileStream(PathFileName, FileMode.Create, FileAccess.Write);                    if (fi.Extension.ToUpper() == ".XLS" || fi.Extension.ToUpper() == ".XLS")                    {                        Export_Excel(fs, dt, gcList);                    }                    else if (fi.Extension.ToUpper() == ".TXT")                    {                        Export_Txt(fs, dt, gcList);                    }                }            }            catch            {            }        }        private static void Export_Excel(FileStream fs, DataTable dt, List<GridColumn> gcList)        {            try            {                //创建Excel文件的对象                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();                NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1"); //添加一个sheet                int GCCount = gcList.Count;                NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);                for (int i = 0; i < GCCount; i++)                {                    string ColName = gcList[i].DataPropertyName;                    if (!dt.Columns.Contains(ColName))                    {                        dt.Columns.Add(ColName, typeof(string));                    }                    row1.CreateCell(i).SetCellValue(gcList[i].HeaderText);                }                int r = 1;                foreach (DataRow dr in dt.Rows)                {                    NPOI.SS.UserModel.IRow erow = sheet1.CreateRow(r);                    for (int i = 0; i < GCCount; i++)                    {                        object o = dr[gcList[i].DataPropertyName];                        erow.CreateCell(i).SetCellValue(o != null ? o.ToString() : "");                    }                    r++;                }                book.Write(fs);                book = null;            }            catch            {            }            finally            {                if (fs != null)                {                    fs.Close();                    fs.Dispose();                }            }        }        private static void Export_Txt(FileStream fs, DataTable dt, List<GridColumn> gcList)        {            try            {                //创建Excel文件的对象                StreamWriter strwriter = new StreamWriter(fs);                string s = "";                bool IsTag = true;                foreach (GridColumn gc in gcList)                {                    string ColName = gc.DataPropertyName;                    if (!dt.Columns.Contains(ColName))                    {                        dt.Columns.Add(ColName, typeof(string));                    }                    if (IsTag)                    {                        IsTag = false;                        s = gc.HeaderText;                    }                    else                    {                        s = s + "," + gc.HeaderText;                    }                }                strwriter.WriteLine(s);                foreach (DataRow dr in dt.Rows)                {                    s = "";                    IsTag = true;                    foreach (GridColumn gc in gcList)                    {                        object o = dr[gc.DataPropertyName];                        string k = (o != null ? o.ToString() : "");                        if (IsTag)                        {                            IsTag = false;                            s = k;                        }                        else                        {                            s = s + "," + k;                        }                    }                    strwriter.WriteLine(s);                }                strwriter.Flush();                strwriter.Close();                strwriter.Dispose();            }            catch            {            }            finally            {                fs.Close();                fs.Dispose();            }        }    }}
 |