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);
}
![](/Images/OutliningIndicators/None.gif)
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";
![](/Images/OutliningIndicators/InBlock.gif)
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
![](/Images/OutliningIndicators/InBlock.gif)
ctl.RenderControl(hw);
![](/Images/OutliningIndicators/InBlock.gif)
Response.Write(tw.ToString());
Response.End();
}2。导入excel写的太麻烦了,弹出窗口可以!用这个导入
Protected Sub toExcel_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)
ToExcel1(tblResults)
End Sub
![](/Images/OutliningIndicators/None.gif)
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"
![](/Images/OutliningIndicators/InBlock.gif)
ctl.Page.EnableViewState = False
Dim tw As System.IO.StringWriter = New System.IO.StringWriter()
Dim hw As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(tw)
![](/Images/OutliningIndicators/InBlock.gif)
ctl.RenderControl(hw)
![](/Images/OutliningIndicators/InBlock.gif)
Response.Write(tw.ToString())
Response.End()
End Sub3.//将数据集导到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[1, 1] =lblReportTime.Text; //合并项
oSheet.Cells[2, 1] = lblReportCount.Text;//合并项
oSheet.Cells[4, 1] = "话机号码" ;//合并项
oSheet.Cells[4, 2] = "月份" ;//合并项
oSheet.Cells[3, 3] = "通话总次数(次)";
oSheet.Cells[3, 7] = "通话总时长(分)";
oSheet.Cells[3,11] = "通话总金额(元)";
oSheet.Cells[4, 3] = "国际";
oSheet.Cells[4, 4] = "国内";
oSheet.Cells[4, 5] = "港澳台";
oSheet.Cells[4, 6] = "合计";
oSheet.Cells[4, 7] = "国际";
oSheet.Cells[4, 8] = "国内";
oSheet.Cells[4, 9] = "港澳台";
oSheet.Cells[4,10] = "合计";
oSheet.Cells[4,11] = "国际";
oSheet.Cells[4,12] = "国内";
oSheet.Cells[4,13] = "港澳台";
oSheet.Cells[4,14] = "合计";
![](/Images/OutliningIndicators/InBlock.gif)
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()) ;
![](/Images/OutliningIndicators/InBlock.gif)
//该范围内设置里面所有单元格的边框
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();
![](/Images/OutliningIndicators/InBlock.gif)
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() ;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
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()) ;
![](/Images/OutliningIndicators/InBlock.gif)
//该范围内设置里面所有单元格的边框
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 ;
![](/Images/OutliningIndicators/InBlock.gif)
oSheet.get_Range("A1","N1").Merge(true);
oSheet.get_Range("A2","N2").Merge(true);
![](/Images/OutliningIndicators/InBlock.gif)
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);
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
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();
![](/Images/OutliningIndicators/InBlock.gif)
System.Runtime.InteropServices.Marshal.ReleaseComObject (excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
![](/Images/OutliningIndicators/InBlock.gif)
oSheet = null;
oWB= null;
excel = null;
![](/Images/OutliningIndicators/InBlock.gif)
GC.Collect();
string test="../../excel-file/" + strFileName;
Response.Redirect(test);
}
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。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)