学习总结——实现生成excel表格(方法二)
生活随笔
收集整理的這篇文章主要介紹了
学习总结——实现生成excel表格(方法二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本方法主要是利用了反射,具體代碼如下:
Code????????public?static?int?ExportTohtmlExcel__dksq_view(string?strsql)
????????{
????????????//貸款申請信息獲得
????????????BLLibrary.BView_jbxx_dksq?viewdal?=?new?BView_jbxx_dksq();
????????????IList<View_jbxx_dksqEntity>?views?=?viewdal.GetView_jbxx_dksqsbyCondition(strsql);
????????????StringBuilder?str?=?new?StringBuilder();
????????????str.Append("<head><meta?http-equiv=Content-Type?content='text/html;?charset=UTF8'></head><body><table???border=1??cellspacing=2???cellpadding=2???width=50%???align=center>");
????????????///輸出表
????????????bool?flag?=?true;
????????????//輸出表頭
????????????string[]?biaoti?=?{?"身份證號",?"姓名",?"性別",?"出生日期",?"戶籍地1",?"戶籍地2",?"現住址1",?"現住址2",?"現詳細地址郵政編碼",?"畢業(yè)中學",?"畢業(yè)中學郵編",?"家庭電話",?"移動電話",?"宿舍電話",?"共同借款人姓名",?"共同借款人身份證號",?"與借款人關系",?"共同借款人戶籍地1",?"共同借款人戶籍地2",?"共同借款人現住址1",?"共同借款人現住址2",?"共同借款人郵政編碼",?"共同借款人工作單位",?"共同借款人職務",?"共同借款人固定電話","大學名稱",??"共同借款人移動電話",?"專業(yè)",?"年級",?"院系",?"學歷",?"學制",?"高校所屬省份",?"借款人學校類別1",?"借款人學校類別2",
??????????????????????????????????"申請成功次數",?"直管縣",?"學校開戶行","學校帳戶名","學校帳號","學校聯(lián)系人","學校聯(lián)系電話","申請原因","申請詳細原因","申請次數","申請貸款金額","申請批準金額","申請期限","申請學年","高校要求報道日期","申請狀態(tài)","院校代碼"};
foreach?(View_jbxx_dksqEntity?view?in?views)
????????????{
????????????????PropertyInfo[]?Properties?=?view.GetType().GetProperties();//利用反射
????????????????//定義表頭
????????????????if?(flag)
????????????????{
????????????????????str.Append("<tr???bgcolor=#cccccc>");
????????????????????for?(int?i?=?0;?i?<?biaoti.Length;?i++)
????????????????????{
????????????????????????str.Append("<td?>"?+?biaoti[i]?+?"</td>");
????????????????????}
????????????????????str.Append("</tr>");
????????????????????flag?=?false;
????????????????}
//輸出表內容
????????????????str.Append("<tr>");
????????????????foreach?(PropertyInfo?pi?in?Properties)//對每個字段屬性進行循環(huán)
????????????????{
????????????????????switch?(pi.Name)//對于不同的屬性進行特定的調整(翻譯、替換、格式化等)
????????????????????{
????????????????????????//學要加"'"防止科學計數法
????????????????????????case?"userid":
????????????????????????case?"gj_sfzid":
????????????????????????case?"jk_homeTelephone":
????????????????????????case?"jk_mobileTelephone":
????????????????????????case?"jk_susheTelephone":
????????????????????????case?"gj_homeTelephone":
????????????????????????case?"gj_mobileTelephone":
????????????????????????case?"xxzh":
????????????????????????case?"xxlxdh":
????????????????????????????{
????????????????????????????????str.Append("<td??style='mso-number-format:\"\\@\"'>"?+?pi.GetValue(view,?null).ToString()?+?"</td>");
????????????????????????????????break;
????????????????????????????}
????????????????????????//日期型轉換
????????????????????????case?"universitystart_date":
????????????????????????case?"jk_BirthDate":
????????????????????????case?"d_dkrq":
????????????????????????case?"d_hkrq":
????????????????????????case?"byrq":
????????????????????????????{
????????????????????????????????string?datestr?=?((DateTime)(pi.GetValue(view,?null))).ToShortDateString();
????????????????????????????????if?(datestr?!=?"9999-1-1")
????????????????????????????????{
????????????????????????????????????datestr?=?"'"?+?datestr;
????????????????????????????????}
????????????????????????????????else
????????????????????????????????{
????????????????????????????????????datestr?=?"";
????????????????????????????????}
????????????????????????????????str.Append("<td>"?+?datestr?+?"</td>");
????????????????????????????????break;
????????????????????????????}
????????????????????????case?"jk_sex":
????????????????????????????{
????????????????????????????????string?jk_sex?=?(string)pi.GetValue(view,?null);
????????????????????????????????if?(jk_sex?==?"1")
????????????????????????????????{
????????????????????????????????????jk_sex?=?"男";
????????????????????????????????}
????????????????????????????????else
????????????????????????????????{
????????????????????????????????????jk_sex?=?"女";
????????????????????????????????}
????????????????????????????????str.Append("<td>"?+?jk_sex?+?"</td>");
????????????????????????????????break;
????????????????????????????}
????????????????????????default:
????????????????????????????{
????????????????????????????????str.Append("<td>"?+?pi.GetValue(view,?null)?+?"</td>");
????????????????????????????????break;
????????????????????????????}//默認直接用取得的值
????????????????????}
????????????????}
????????????????str.Append("</tr>");
????????????}
????????????str.Append("</table></body></html>");
????????????//輸出文檔
????????????HttpContext.Current.Response.ClearContent();
????????????HttpContext.Current.Response.AddHeader("content-disposition",?"attachment;?filename=MyExcel_dksq.xls");
????????????HttpContext.Current.Response.ContentEncoding?=?System.Text.Encoding.UTF8;
????????????HttpContext.Current.Response.ContentType?=?"application/excel";
????????????HttpContext.Current.Response.Write(str);
????????????HttpContext.Current.Response.End();
????????????return?0;
????????}
?另外還有個方法,但是不是很推薦:
Code????????public?static?int?ExportToExcel_FirmReportRp1(DataSet?ds)
????????{
????????????//報表文件名
????????????string?reportName?=?HttpContext.Current.Server.MapPath(REPORTPATH)?+?RP1;
????????????//樣表文件名
????????????string?templateName?=?HttpContext.Current.Server.MapPath(TEMPLATEPATH)?+?RP1;
????????????#region?是否存在樣表
????????????if?(!File.Exists(templateName))
????????????{
????????????????return?-1;
????????????}
????????????#endregion
????????????#region?如果已經存在此報表,則刪除報表,將樣表復制到報表文件
????????????if?(File.Exists(reportName))?File.Delete(reportName);
????????????File.Copy(templateName,?reportName);
????????????#endregion
????????????Microsoft.Office.Interop.Excel.Application?excel?=?new?Microsoft.Office.Interop.Excel.Application();
????????????object?oMissiong?=?System.Reflection.Missing.Value;
????????????excel.Visible?=?false;//true?or?false?
????????????Microsoft.Office.Interop.Excel.Workbook?book?=?excel.Workbooks.Open(reportName,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong,?oMissiong);
????????????Microsoft.Office.Interop.Excel.Worksheet?sheet?=?(Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
????????????System.Data.DataTable?table?=?ds.Tables[0];
????????????int?rowIndex?=?12;???????????????//總計所在行號
????????????Range?rng;
????????????foreach?(DataRow?row?in?table.Rows)
????????????{
????????????????rowIndex++;
????????????????//"期末總人口"
????????????????rng?=?sheet.get_Range("B"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["B"].ToString();
????????????????//"期初總人口"
????????????????rng?=?sheet.get_Range("C"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["C"].ToString();
????????????????rng?=?sheet.get_Range("D"?+?rowIndex,?oMissiong);
????????????????rng.Formula?=?"=SUM(E"?+?rowIndex?+?":F"?+?rowIndex?+?")";
????????????????//出生人數-男
????????????????rng?=?sheet.get_Range("E"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["E"].ToString();
????????????????//出生人數-女
????????????????rng?=?sheet.get_Range("F"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["F"].ToString();
????????????}
????????????#region?處理最后一行的簽名
????????????rowIndex++;
????????????rng?=?sheet.get_Range("A"?+?rowIndex,?oMissiong);
????????????rng.Value2?=?"填表日期:";
????????????//合并BC兩列,填充日期
????????????rng?=?sheet.get_Range("B"?+?rowIndex,?"C"?+?rowIndex);
????????????rng.Merge(true);
????????????rng.Value2?=?DateTime.Now.ToShortDateString();
????????????//主管領導
????????????rng?=?sheet.get_Range("F"?+?rowIndex,?oMissiong);
????????????rng.Value2?=?"主管領導:";
????????????//填表人
????????????rng?=?sheet.get_Range("L"?+?rowIndex,?oMissiong);
????????????rng.Value2?=?"填表人:";
????????????#endregion
????????????book.Save();
????????????book.Close(true,?reportName,?true);????????????
????????????excel.Quit();
????????????KillProcess.Kill(excel);//kill掉當前excel進程
????????????excel?=?null;
????????????return?0;
}
?
轉載于:https://www.cnblogs.com/lostcosta/archive/2009/10/29/1592346.html
總結
以上是生活随笔為你收集整理的学习总结——实现生成excel表格(方法二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Session详解
- 下一篇: 空城和镇魂冲突么