http://search.csdn.net/Expert/topic/2346/2346423.xml?temp=.3901941
http://search.csdn.net/Expert/topic/2387/2387301.xml?temp=3.222293E-02
http://search.csdn.net/Expert/topic/2581/2581246.xml?temp=.9223444
http://search.csdn.net/Expert/topic/2414/2414749.xml?temp=5.735415E-02
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q302096
1。
protected void toExcel_Click(object sender,EventArgs e) 
      

         ToExcel(parentList); 
      }
 

      
private void ToExcel(System.Web.UI.Control ctl) 
      

         Response.Charset
="GB2312"
         Response.AppendHeader(
"Content-Disposition","attachment;filename=Excel.xls");  
         Response.ContentEncoding 
= System.Text.Encoding.UTF8; 
         Response.ContentType 
= "application/ms-excel"

         ctl.Page.EnableViewState 
= false
         System.IO.StringWriter tw 
= new System.IO.StringWriter(); 
         System.Web.UI.HtmlTextWriter hw 
= new HtmlTextWriter(tw); 

         ctl.RenderControl(hw); 

         Response.Write(tw.ToString()); 
         Response.End(); 
      }
2。导入excel写的太麻烦了,弹出窗口可以!用这个导入
  Protected Sub toExcel_Click(ByVal sender As ObjectByVal e As ImageClickEventArgs)
            ToExcel1(tblResults)
        
End Sub


        
Sub ToExcel1(ByVal ctl As System.Web.UI.Control)
            Response.Charset 
= "GB2312"
            Response.AppendHeader(
"Content-Disposition""attachment;filename=totalreport.xls")
            Response.ContentEncoding 
= System.Text.Encoding.UTF8
            Response.ContentType 
= "application/ms-excel"

            ctl.Page.EnableViewState 
= False
            
Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
            
Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(tw)

            ctl.RenderControl(hw)

            Response.Write(tw.ToString())
            Response.End()
        
End Sub
3.//将数据集导到excel 以 XLS 格式里
private void DataSetToExcelXls(DataSet ds) 
        
{    
            
用例说明
            
            Random rd
=new Random(int.Parse(DateTime.Now.ToString("MMddhhmmss")));
            
string strFileName =DateTime.Now.ToString("yyyyMMdd")+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+txtBeginTime.Text.Trim()+txtFinishTime.Text.Trim()+rd.Next(999999).ToString()+ ".XLS";
            
            
//Excel.Application excel= new Excel.ApplicationClass();//Excel.Application();
            Excel.Application excel ;
            
//excel = new Excel.Application();
            excel = new Excel.ApplicationClass();
            Excel.WorkbookClass oWB;
            Excel.Worksheet oSheet;
            oWB 
= (Excel.WorkbookClass)(excel.Workbooks.Add(true));
            oSheet 
= (Excel.Worksheet)oWB.ActiveSheet;
            
            oSheet.Cells[
11=lblReportTime.Text;    //合并项
            oSheet.Cells[21= lblReportCount.Text;//合并项
            oSheet.Cells[41= "话机号码" ;//合并项
            oSheet.Cells[42= "月份" ;//合并项
            oSheet.Cells[33= "通话总次数(次)";
            oSheet.Cells[
37= "通话总时长(分)";
            oSheet.Cells[
3,11= "通话总金额(元)";
            oSheet.Cells[
43= "国际";
            oSheet.Cells[
44= "国内";
            oSheet.Cells[
45= "港澳台";
            oSheet.Cells[
46= "合计";
            oSheet.Cells[
47= "国际";
            oSheet.Cells[
48= "国内";
            oSheet.Cells[
49= "港澳台";
            oSheet.Cells[
4,10= "合计";
            oSheet.Cells[
4,11= "国际";
            oSheet.Cells[
4,12= "国内";
            oSheet.Cells[
4,13= "港澳台";
            oSheet.Cells[
4,14= "合计";

            oSheet.get_Range(
"A1","N4").Font.Size= 9 ;
            oSheet.get_Range(
"A1","N4").Font.Bold = true;
            oSheet.get_Range(
"A1","N4").VerticalAlignment =  Excel.XlVAlign.xlVAlignCenter;
            oSheet.get_Range(
"A1","N4").HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            
            
//该范围内设置外面一层边框
            oSheet.get_Range("A1","N4").BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;

            
//该范围内设置里面所有单元格的边框
            oSheet.get_Range("A1","N4").Borders.Weight = Excel.XlBorderWeight.xlThin ;
            oSheet.get_Range(
"A1","N4").Borders.Color = Color.Black.ToArgb() ;
            
//范围内单元格添加背景颜色
            oSheet.get_Range("A1","N2").Cells.Interior.Color =Color.FromArgb(153,255,204).ToArgb();
            oSheet.get_Range(
"A1","N2").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
        
            oSheet.get_Range(
"A3","N4").Cells.Interior.Color = Color.LemonChiffon.ToArgb();
            oSheet.get_Range(
"A3","N4").Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;
            
            DataTable dt 
=ds.Tables[0];
            DataRow dr;
            
int count=dt.Rows.Count+5;
            
string strExcelLength = "N"+count.ToString().Trim();

            
int I,J;
            
for(I = 5;I< count;I++)
            
{                
                dr
=dt.Rows[I-5];
                
                
for(J = 1;J<(dt.Columns.Count +1) ;J++)
                
{
                    oSheet.Cells[I,J] 
= dr[J-1].ToString().Trim() ;
                
                }

                
            }


            oSheet.get_Range(
"A5",strExcelLength).Font.Size= 9 ;
            oSheet.get_Range(
"A5",strExcelLength).Font.Bold = false;
            oSheet.get_Range(
"A5",strExcelLength).VerticalAlignment =  Excel.XlVAlign.xlVAlignCenter;
            oSheet.get_Range(
"A5",strExcelLength).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            
//该范围内设置外面一层边框
            oSheet.get_Range("A5",strExcelLength).BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin ,Excel.XlColorIndex.xlColorIndexAutomatic,Color.Black.ToArgb()) ;

            
//该范围内设置里面所有单元格的边框
            oSheet.get_Range("A5",strExcelLength).Borders.Weight = Excel.XlBorderWeight.xlThin ;
            oSheet.get_Range(
"A5",strExcelLength).Borders.Color = Color.Black.ToArgb() ;
            
//范围内单元格添加背景颜色
            oSheet.get_Range("A5",strExcelLength).Cells.Interior.Color = Color.PaleTurquoise.ToArgb();
            oSheet.get_Range(
"A5",strExcelLength).Cells.Interior.Pattern = Excel.XlBackground.xlBackgroundAutomatic ;

            
            oSheet.get_Range(
"A1","N1").Merge(true);
            oSheet.get_Range(
"A2","N2").Merge(true);            

            oSheet.get_Range(
"A3","A4").Merge(true);
            oSheet.get_Range(
"B3","B4").Merge(true);
            oSheet.get_Range(
"C3","F3").Merge(true);
            oSheet.get_Range(
"G3","J3").Merge(true);
            oSheet.get_Range(
"K3","N3").Merge(true);


            
string path=Server.MapPath("."+ "\\..\\..\\excel-file\\" + strFileName;            
            
            oWB.SaveAs(path,Missing.Value,
"","",true,false,Excel.XlSaveAsAccessMode.xlNoChange,1,false,Missing.Value,Missing.Value);
            
            
//oWB.SaveCopyAs(path);                
            
            oWB.Close(
false,null,null);
            excel.Workbooks.Close();            
            excel.Quit();

            System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
            System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);

            oSheet 
= null;
            oWB
= null;
            excel 
= null;

            GC.Collect();        
                    
            
string test="../../excel-file/" + strFileName;
            Response.Redirect(test);        
        }
posted on 2006-03-06 10:15  星火燎原  阅读(1524)  评论(2编辑  收藏  举报