在前臺(tái)頁面中先插入一個(gè)ASPxHtmlEditor控件,名為ASPxHtmlEditor1。 我用的Dev版本為14.1
在后臺(tái)插入如下代碼
1conststringCSS="style='text-align:justify;"http://兩端對(duì)齊2+"text-justify:inter-ideograph;"3+"text-indent:2em;"http://首行縮進(jìn)2字符4+"line-height:1.25;"http://1.25倍行距5+"margin-top:0;margin-bottom:0;"http://段前段后0行6+"font-size:12pt;"http://字體:小四7+"font-family:TimesNewRoman,宋體;'"; //中文字體:宋體,西文字體:TimesNewRoman8910ASPxHtmlEditor1.Html="<p"+css+">";//段落用標(biāo)簽p標(biāo)記11for(inti=0;i<30;i++)12ASPxHtmlEditor1.Html+="測試文本123abCD"; //這是內(nèi)容13ASPxHtmlEditor1.Html+="</p>上述代碼實(shí)現(xiàn)了對(duì)文字的格式化,可以基本滿足生成一般報(bào)告的格式需要。
對(duì)于ASPxHtmlEditor控件,無法直接設(shè)置導(dǎo)出Word或其他格式文件的頁面(頁邊距、紙張大小等),需借助RichEditDocumentServer來轉(zhuǎn)存實(shí)現(xiàn)。
添加如下引用:
1usingDevExpress.XtraRichEdit;2usingDevExpress.XtraRichEdit.API.Native;插入下面兩個(gè)函數(shù)
1privatevoidSetPrintOptions(IRichEditDocumentServerrichedit)//設(shè)置格式
2{3foreach(Section_sectioninrichedit.Document.Sections)4{5_section.Page.PaperKind=System.Drawing.Printing.PaperKind.A4;//A4紙6_section.Page.Landscape=false;//豎版7_section.Margins.Left=295f;//左側(cè)頁邊距2.5cm8_section.Margins.Right=295f;9_section.Margins.Top=295f;10_section.Margins.Bottom=295f;1112}13}1415protectedvoidPushToBrowser(stringfileName)//導(dǎo)出文件16{1718FileStreamfs=newFileStream(fileName,FileMode.Open);19byte[]bytes=newbyte[(int)fs.Length];20fs.Read(bytes,0,bytes.Length);21fs.Close();22if(File.Exists(fileName))23File.Delete(fileName);2425Response.ContentType="application/octet-stream";26Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));27Response.BinaryWrite(bytes);28Response.Flush();29Response.End();30}在執(zhí)行導(dǎo)出的事件(如Button_Click事件)中添加如下代碼
1stringoutputFileName="newtext.doc";//導(dǎo)出文件的名稱
23FileStreamfs=newFileStream("test.rtf",FileMode.Create);4ASPxHtmlEditor1.Export(DevExpress.Web.ASPxHtmlEditor.HtmlEditorExportFormat.Rtf,fs); //注意此處 先導(dǎo)出Rtf格式5fs.Close();6fs.Close();78RichEditDocumentServersrv=newRichEditDocumentServer();9srv.LoadDocument("test.rtf",DocumentFormat.Rtf);10srv.BeginUpdate();11SetPrintOptions(srv);12srv.EndUpdate();13srv.SaveDocument(outputFileName,DocumentFormat.Doc); //再導(dǎo)出Doc格式 如果導(dǎo)出Openxml(.Docx)格式,則中文字體丟失1415if(File.Exists("test.rtf"))16File.Delete("test.rtf");1718PushToBrowser(outputFileName);輸出的文本如下:
p.s.關(guān)于頁邊距設(shè)置成cm的換算
試了幾組數(shù)據(jù),做了個(gè)3次擬合,先湊合用吧。(x是期望設(shè)置的厘米數(shù) 如2.5cm y就是程序的中用到的參數(shù) 如295 )
轉(zhuǎn)載請注明出處http://www.companysz.com/LFDX/p/4688938.html新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注