在.NET中excel导出方法汇总(收集)
生活随笔
收集整理的這篇文章主要介紹了
在.NET中excel导出方法汇总(收集)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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()Sub?toExcel_Click(ByVal?sender?As?Object,?ByVal?e?As?ImageClickEventArgs)
????????????ToExcel1(tblResults)
????????End?Sub
????????Sub?ToExcel1()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?Sub3.//將數據集導到excel 以 XLS 格式里
private?void?DataSetToExcelXls(DataSet?ds)?
????????{????
????????????用例說明#region?用例說明
????????????/**//*?
????????????【輸入】?
?????????????sender:???????
?????????????e:?????????????
????????????【輸出】
?????????????無
????????????【流程定義】
?????????????1、將生成的DataSet的數據導到指定的Execl文件里,并顯示出來
????????????【可選流程】
????????????????????????????
????????????【問題】
????????????
????????????*/
????????????#endregion
????????????
????????????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]?=?"合計";
????????????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);????????
????????}
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()Sub?toExcel_Click(ByVal?sender?As?Object,?ByVal?e?As?ImageClickEventArgs)
????????????ToExcel1(tblResults)
????????End?Sub
????????Sub?ToExcel1()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?Sub3.//將數據集導到excel 以 XLS 格式里
private?void?DataSetToExcelXls(DataSet?ds)?
????????{????
????????????用例說明#region?用例說明
????????????/**//*?
????????????【輸入】?
?????????????sender:???????
?????????????e:?????????????
????????????【輸出】
?????????????無
????????????【流程定義】
?????????????1、將生成的DataSet的數據導到指定的Execl文件里,并顯示出來
????????????【可選流程】
????????????????????????????
????????????【問題】
????????????
????????????*/
????????????#endregion
????????????
????????????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]?=?"合計";
????????????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);????????
????????}
轉載于:https://www.cnblogs.com/RedStarLiu/archive/2006/03/06/343692.html
總結
以上是生活随笔為你收集整理的在.NET中excel导出方法汇总(收集)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python win32模块详解_pyt
- 下一篇: display属性_CSS之使用disp