学习总结——实现生成excel表格(方法二)
生活随笔
收集整理的這篇文章主要介紹了
学习总结——实现生成excel表格(方法二)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
本方法主要是利用了反射,具體代碼如下:
Code????????public?static?int?ExportTohtmlExcel__dksq_view(string?strsql)
????????{
????????????//貸款申請(qǐng)信息獲得
????????????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?=?{?"身份證號(hào)",?"姓名",?"性別",?"出生日期",?"戶籍地1",?"戶籍地2",?"現(xiàn)住址1",?"現(xiàn)住址2",?"現(xiàn)詳細(xì)地址郵政編碼",?"畢業(yè)中學(xué)",?"畢業(yè)中學(xué)郵編",?"家庭電話",?"移動(dòng)電話",?"宿舍電話",?"共同借款人姓名",?"共同借款人身份證號(hào)",?"與借款人關(guān)系",?"共同借款人戶籍地1",?"共同借款人戶籍地2",?"共同借款人現(xiàn)住址1",?"共同借款人現(xiàn)住址2",?"共同借款人郵政編碼",?"共同借款人工作單位",?"共同借款人職務(wù)",?"共同借款人固定電話","大學(xué)名稱",??"共同借款人移動(dòng)電話",?"專業(yè)",?"年級(jí)",?"院系",?"學(xué)歷",?"學(xué)制",?"高校所屬省份",?"借款人學(xué)校類別1",?"借款人學(xué)校類別2",
??????????????????????????????????"申請(qǐng)成功次數(shù)",?"直管縣",?"學(xué)校開戶行","學(xué)校帳戶名","學(xué)校帳號(hào)","學(xué)校聯(lián)系人","學(xué)校聯(lián)系電話","申請(qǐng)?jiān)?/span>","申請(qǐng)?jiān)敿?xì)原因","申請(qǐng)次數(shù)","申請(qǐng)貸款金額","申請(qǐng)批準(zhǔn)金額","申請(qǐng)期限","申請(qǐng)學(xué)年","高校要求報(bào)道日期","申請(qǐng)狀態(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;
????????????????}
//輸出表內(nèi)容
????????????????str.Append("<tr>");
????????????????foreach?(PropertyInfo?pi?in?Properties)//對(duì)每個(gè)字段屬性進(jìn)行循環(huán)
????????????????{
????????????????????switch?(pi.Name)//對(duì)于不同的屬性進(jìn)行特定的調(diào)整(翻譯、替換、格式化等)
????????????????????{
????????????????????????//學(xué)要加"'"防止科學(xué)計(jì)數(shù)法
????????????????????????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;
????????????????????????????}
????????????????????????//日期型轉(zhuǎn)換
????????????????????????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;
????????????????????????????}//默認(rèn)直接用取得的值
????????????????????}
????????????????}
????????????????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;
????????}
?另外還有個(gè)方法,但是不是很推薦:
Code????????public?static?int?ExportToExcel_FirmReportRp1(DataSet?ds)
????????{
????????????//報(bào)表文件名
????????????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?如果已經(jīng)存在此報(bào)表,則刪除報(bào)表,將樣表復(fù)制到報(bào)表文件
????????????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;???????????????//總計(jì)所在行號(hào)
????????????Range?rng;
????????????foreach?(DataRow?row?in?table.Rows)
????????????{
????????????????rowIndex++;
????????????????//"期末總?cè)丝?#34;
????????????????rng?=?sheet.get_Range("B"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["B"].ToString();
????????????????//"期初總?cè)丝?#34;
????????????????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?+?")";
????????????????//出生人數(shù)-男
????????????????rng?=?sheet.get_Range("E"?+?rowIndex,?oMissiong);
????????????????rng.Value2?=?row["E"].ToString();
????????????????//出生人數(shù)-女
????????????????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();
????????????//主管領(lǐng)導(dǎo)
????????????rng?=?sheet.get_Range("F"?+?rowIndex,?oMissiong);
????????????rng.Value2?=?"主管領(lǐng)導(dǎo):";
????????????//填表人
????????????rng?=?sheet.get_Range("L"?+?rowIndex,?oMissiong);
????????????rng.Value2?=?"填表人:";
????????????#endregion
????????????book.Save();
????????????book.Close(true,?reportName,?true);????????????
????????????excel.Quit();
????????????KillProcess.Kill(excel);//kill掉當(dāng)前excel進(jìn)程
????????????excel?=?null;
????????????return?0;
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/lostcosta/archive/2009/10/29/1592346.html
總結(jié)
以上是生活随笔為你收集整理的学习总结——实现生成excel表格(方法二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Session详解
- 下一篇: 向左滚动,每次滚动的长度可以设置,然后暂