日期:2014-05-20  浏览次数:20938 次

asp.net 动态操作EXCEL的问题~!跟winform有很大区别!
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                SqlConnection   conn   =   new   SqlConnection( "server=.;uid=sa;pwd=123456;database=test; ");
                DataSet   ds   =   SqlHelper.ExecuteDataset(conn,   CommandType.Text,   "select   *   from   [user] ");
                System.Data.DataTable   tmpDataTable   =   ds.Tables[0];

                Excel.Application   excel   =   new   Excel.ApplicationClass();

                Excel._Workbook   exb;
                Excel._Worksheet   exs;
                exb   =   excel.Workbooks.Add(true);
                exs   =   (Excel._Worksheet)exb.ActiveSheet;

                int   colIndex   =   0;
                int   rowIndex   =   1;

                int   rowSum   =   tmpDataTable.Rows.Count;
                int   ColSum   =   tmpDataTable.Columns.Count;

                foreach   (DataColumn   dc   in   tmpDataTable.Columns)
                {
                        colIndex++;
                        excel.Cells[rowIndex,   colIndex]   =   dc.ColumnName;
                }


                foreach   (DataRowView   row   in   tmpDataTable.DefaultView)
                {
                        rowIndex++;
                        colIndex   =   0;
                        foreach   (DataColumn   col   in   tmpDataTable.Columns)
                        {
                                colIndex++;
                                if   (col.DataType   ==   System.Type.GetType( "System.DateTime "))
                                {
                                        excel.Cells[rowIndex,   colIndex]   =   (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString( "yyyy-MM-dd ");
                                }