以下示例將完成批量導出word的功能,由於涉及到批量,爲了頁面不‘假死’,用到了多線程。至於線程最多能有多少個,網上說沒有限制,當時也懶得考慮了,直接多少份就用多少線程。 導出借用的是水晶報表(Crystal Reports)的現有資源,主要就是穩定,比前一篇導出word的不確定因素相比少了很多。最重要的是,這個頁面很複雜,使用了三個子報表,水晶報表的強大之處完全體現出來了。 如果仔細看,你會發現,不只是導出word,最下面的方法是選擇導出的格式,有5種,我所知道常用的,除了Cvs格式都有了,能夠滿足你的要求了吧。。
/// <summary> |
/// 多線程操作 |
/// </summary> |
class threadbody |
{ |
Bill_Word billWord; //類 |
int threadId;//線程編號 |
string thCustId; //客戶編號 |
string path; |
public threadbody(Bill_Word _billWord, int _threadId, string _thCustId, string _path) |
{ |
billWord = _billWord; |
threadId = _threadId; |
thCustId = _thCustId; |
path = _path; |
} |
/// <summary> |
/// 線程批量生成 |
/// </summary> |
public void ThreadCreate() |
{ |
ReportDocument myReport = new ReportDocument(); |
string reportPath = System.Threading.Thread.GetDomain().BaseDirectory + "Reports\\rpt\\rp_word.rpt"; |
myReport.Load(reportPath); |
//數據填充 |
DataSet fillDS = billWord.GetFill(thCustId); |
myReport.SetDataSource(fillDS); |
string fileName = fillDS.Tables[3].Rows[0][7].ToString() + DateTime.Now.ToString("yyyyMMddhhssmm"); |
if (billWord.Export(path + fileName, "doc", myReport, thCustId)) //如果導出成功,保存記錄 |
{ |
clsReports.fExportFilesAction(1,"",fileName + ".doc",fillDS.Tables[3].Rows[0][6].ToString(),thCustId,DateTime.Now,false,Convert.ToDateTime(fillDS.Tables[3].Rows[0][0]).Month + "月"); |
billWord.BArr_threadw[threadId] = true;//表明此線程結束。 |
} |
} |
} |
/// <summary> |
/// 頁面類(經過精簡,只要相關內容) |
/// </summary> |
public partial class Bill_Word : System.Web.UI.Page |
{ |
public bool[] BArr_threadw; //每個線程結束標誌 |
public bool BFinish; |
public string[] custIds; |
public string mes = "現在沒信息啦"; |
private StringBuilder sb_suc = new StringBuilder(""); |
protected void Page_Load(object sender, EventArgs e) |
{ |
if (Request["action"] == "1") //批量生成 |
{ |
//Thread thCreate = new Thread(new ThreadStart(ThreadCreate)); |
//thCreate.Start(); |
//ThreadCreate(); |
custIds = Request.Cookies["Bill_custs"].Value.Split(','); |
//定義線程數組,啓動接收線程 |
Thread[] threadSend = new Thread[custIds.Length]; //多線程 |
threadbody[] thBody = new threadbody[custIds.Length]; //類 |
for (int j = 0; j < custIds.Length; j++) |
{ |
thBody[j] = new threadbody(this, j, custIds[j],Server.MapPath("..\\Bill\\Send\\")); |
threadSend[j] = new Thread(new ThreadStart(thBody[j].ThreadCreate)); |
threadSend[j].Start(); |
} |
BArr_threadw = new bool[custIds.Length]; |
MesShow(); |
} |
} |
/// <summary> |
/// 所有線程結束提示 |
/// </summary> |
public void MesShow() |
{ |
while (true)//等待 |
{ |
BFinish = true; |
for (int i = 0; i <custIds.Length; i++) |
{ |
if (BArr_threadw[i] == false)//有未結束線程,等待 |
{ |
BFinish = false; |
Thread.Sleep(100); |
break; |
} |
} |
if (BFinish == true)//所有線程均已結束,停止等待, |
{ |
break; |
} |
} |
if (BFinish == true) |
{ |
//操作提示 |
int total = custIds.Length; |
int suc = sb_suc.ToString().Split(',').Length; |
int fail = total - suc; |
//btnSend.AddScript(clsSystem.fGetExtMsgAlert("系統提示", string.Format("總共發送郵件{0}封,成功發送郵件{1}封,失敗{2}封", total, suc, fail))); |
//GridFiles.Reload(); |
mes = string.Format("總共生成對賬單{0}份,成功生成{1}份,失敗{2}份", total, suc, fail); |
Response.Write("<script>alert('"+mes+"');</script>"); |
} |
} |
/// <summary> |
/// 導出到服務器端 |
/// </summary> |
/// <param name="FileName">文件保存路徑</param> |
/// <param name="Ext">擴展名(doc.pdf.rtf.xls.html)</param> |
/// <param name="_report"></param> |
public bool Export(string FileName, string Ext,ReportDocument _report,string Id) |
{ |
try |
{ |
ExportOptions exportOptions = new ExportOptions(); |
DiskFileDestinationOptions diskOptions = ExportOptions.CreateDiskFileDestinationOptions(); |
exportOptions.ExportFormatType = GetExportFormatType(Ext); |
exportOptions.ExportDestinationType = ExportDestinationType.DiskFile; |
diskOptions.DiskFileName = FileName + "." + Ext; |
exportOptions.ExportDestinationOptions = diskOptions; |
_report.Export(exportOptions); |
if (sb_suc.ToString() == "") |
sb_suc.Append(Id); |
else |
sb_suc.Append("," + Id); |
return true; |
} |
catch |
{ |
return false; |
} |
} |
/// <summary> |
/// 導出格式 |
/// </summary> |
/// <param name="ext"></param> |
/// <returns></returns> |
private ExportFormatType GetExportFormatType(string ext) |
{ |
switch (ext) |
{ |
case "pdf": |
return ExportFormatType.PortableDocFormat; |
case "rtf": |
return ExportFormatType.RichText; |
case "doc": |
return ExportFormatType.WordForWindows; |
case "xls": |
return ExportFormatType.Excel; |
case "html": |
return ExportFormatType.HTML32; |
default: |
return ExportFormatType.NoFormat; |
} |
} |
} |
多線程導出word
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.