日期:2014-05-18  浏览次数:20794 次

求C#WINDOWS里面的dateset 导出到excel
如题
目前我写了一中方法,但是导出太慢了
求一种快的导出方法
下面是我的代码,如果有谁有速度快点的导出方法,请告诉下!
谢谢!

C# code

public static void OutputExceltemp(DataView dv,string str,EmicroWin.EmiList list,bool isTotal)
        {
            try
            {
                OutputExceltemp(dv,str,list.TheListView,isTotal);
            }
            catch
            {
               MessageBox.Show("请确保安装Office软件,否则不能导出Excel!");
               return;
            }
        }
        public static void OutputExceltemp(DataView dv,string str,EmicroWin.EmiListView listview,bool isTotal)
        {
            ArrayList cs=new ArrayList();
            foreach(EmicroWin.EmiColumnHeader header in listview.TheColumns)
            {
                string field=header.DataField;
                if(field==null||field=="")continue;

                dv.Table.Columns[field].Caption=header.Text;
                cs.Add(field);
            }
            OutputExceltemp(dv,str,(string[])cs.ToArray(typeof(string)),isTotal);
        }
        public static void OutputExceltemp(DataView dv,string str,bool isTotal)
        {
            ArrayList cs=new ArrayList();
            foreach(DataColumn c in dv.Table.Columns)
            {
                cs.Add(c.ColumnName);
            }
            OutputExceltemp(dv,str,(string[])cs.ToArray(typeof(string)),isTotal);
        }
        public static void OutputExceltemp(DataView dv,string str,string columns,bool isTotal)
        {
            OutputExceltemp(dv,str,columns.Split(",".ToCharArray()),isTotal);
        }
//比较正规的导出
        public static void OutputExceltemp(DataView dv,string str,string[] columns,bool isTotal)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            Excel.Application excel;
            int rowIndex=4;
            int colIndex=0;

            Excel._Workbook xBk;
            Excel._Worksheet xSt;

            excel= new Excel.ApplicationClass();;
            xBk = excel.Workbooks.Add(true);
            xSt = (Excel._Worksheet)xBk.ActiveSheet;

            //
            //取得标题
            //

            //foreach(DataColumn col in dv.Table.Columns)
            foreach(string columnname in columns)
            {
                DataColumn col=dv.Table.Columns[columnname];

                colIndex++;
                excel.Cells[4,colIndex] = col.Caption;
                xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
            }

            decimal[] sums=new decimal[dv.Table.Columns.Count];

            //
            //取得表格中的数据
            //
            foreach(DataRowView row in dv)
            {
                rowIndex ++;
                colIndex = 0;
                foreach(string columnname in columns)
                {
                    DataColumn col=dv.Table.Columns[columnname];

                    colIndex++;

                    if(col.DataType == System.Type.GetType("System.DateTime"))
                    {
                        excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
                        xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
                    }
                    else if(col.DataType == System.Type.GetType("System.String"))
                    {
                        excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
                        xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
                    }
                    else