Npoi205导出word合并行和合并列.docx

上传人:b****7 文档编号:9738759 上传时间:2023-02-06 格式:DOCX 页数:55 大小:26.15KB
下载 相关 举报
Npoi205导出word合并行和合并列.docx_第1页
第1页 / 共55页
Npoi205导出word合并行和合并列.docx_第2页
第2页 / 共55页
Npoi205导出word合并行和合并列.docx_第3页
第3页 / 共55页
Npoi205导出word合并行和合并列.docx_第4页
第4页 / 共55页
Npoi205导出word合并行和合并列.docx_第5页
第5页 / 共55页
点击查看更多>>
下载资源
资源描述

Npoi205导出word合并行和合并列.docx

《Npoi205导出word合并行和合并列.docx》由会员分享,可在线阅读,更多相关《Npoi205导出word合并行和合并列.docx(55页珍藏版)》请在冰豆网上搜索。

Npoi205导出word合并行和合并列.docx

Npoi205导出word合并行和合并列

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.HtmlControls;

usingSystem.Text;

usingSystem.Data;

usingSystem.Collections;

usingSystem.IO;

usingNPOI;

usingNPOI.HSSF.UserModel;

usingDAL=CC.DAL;

usingCC.Common;

usingBLL=CC.BLL;

usingNPOI.XWPF.UserModel;

usingNPOI.OpenXml4Net;

usingNPOI.OpenXmlFormats.Wordprocessing;

/********************

*功能:

食谱分析报告导出

*编写人:

*日期:

2014.5.27

*nopi版本:

2.0.5.0

********************/

publicpartialclassExportFoodCheckList:

BasePage

{

#region//加载模板

protectedvoidPage_Load(objectsender,EventArgse)

{

if(Request["RecweekId"]!

=null)

{

stringtype=Request["type"].ToString();

intRecweekId=int.Parse(Request["RecweekId"].ToString());

ViewState["HidRecipwweekId"]=RecweekId.ToString();

if(type=="Rec")

{

XWPFDocumentdoc=HtmlExport(Int32.Parse(ViewState["HidRecipwweekId"].ToString()),0);//.Replace("

stringwordFile=Server.MapPath("模板.docx");

FileInfofile=newFileInfo(wordFile);

if(file.Exists)

{

FileStreamout1=newFileStream(wordFile,FileMode.Create);

doc.Write(out1);

out1.Close();

openWindowExport(wordFile,"食谱.docx");

}

}

else

{

XWPFDocumentdoc=newXWPFDocument();

stringwordFile=Server.MapPath("模板.docx");

FileInfofile=newFileInfo(wordFile);

if(file.Exists){

doc=tbGetReport(Int32.Parse(ViewState["HidRecipwweekId"].ToString()),doc);

FileStreamout1=newFileStream(wordFile,FileMode.Create);

doc.Write(out1);

out1.Close();

openWindowExport(wordFile,"食谱分析报告.docx");

}

}

}

}

publicstaticstringopenWindowExport(stringstrFileName,stringdownfile)

{

stringstrReutrn="";

try

{

FileInfoDownloadFile=newFileInfo(strFileName);

System.Web.HttpContext.Current.Response.Clear();

System.Web.HttpContext.Current.Response.ClearHeaders();

System.Web.HttpContext.Current.Response.Buffer=false;

System.Web.HttpContext.Current.Response.ContentType="application/octet-stream";

System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="

+System.Web.HttpUtility.UrlEncode(downfile,System.Text.Encoding.UTF8));

System.Web.HttpContext.Current.Response.AppendHeader("Content-Length",DownloadFile.Length.ToString());

System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);

}

catch(ExceptionEx)

{

strReutrn=Ex.Message;

}

finally

{

System.Web.HttpContext.Current.Response.Flush();

System.Web.HttpContext.Current.Response.End();

}

returnstrReutrn;

}

#endregion

#region//导出食谱

privateXWPFDocumentHtmlExport(intrecpweekId,intdispalyMaterial)

{

XWPFDocumentdoc=newXWPFDocument();

XWPFSettingsset=newXWPFSettings();

StringBuilderstr=newStringBuilder();

//星期

DataTabledtWeek=DAL.R_RecipeMeal.GetRecipeWeek(recpweekId).Tables[0];

DataTabledt=newDataTable();

DataTabledt_weekday=newDataTable();

dt=newCC.DAL.R_RecipeWeekMain().GetRecWeekDetailConnMealType(recpweekId).Tables[0];

dt=RCC(dt);

HashtablehRQtable=newHashtable();

stringweekstart="0";

stringweekend="0";

stringstrtitle="周食谱";

if(dispalyMaterial==1)

strtitle="带量食谱";

dt_weekday=newCC.DAL.R_RecipeWeekMain().GetRecSevenDay(recpweekId).Tables[0];

if(dt_weekday.Rows.Count>0)

{

//设置顶部title

weekstart=dt_weekday.Rows[0]["weekstart"].ToString();

weekend=dt_weekday.Rows[0]["weekend"].ToString();

//设置顶部title

XWPFParagraphp1=doc.CreateParagraph();

XWPFRunr1=p1.CreateRun();

r1.SetBold(true);

r1.SetText(dt_weekday.Rows[0]["name"].ToString()+strtitle);

r1.SetBold(true);

r1.SetFontFamily("Courier");

p1.SetAlignment(ParagraphAlignment.CENTER);

r1.SetFontSize(25);

p1=doc.CreateParagraph();

p1.SetAlignment(ParagraphAlignment.CENTER);

r1.SetFontSize(15);

r1=p1.CreateRun();

r1.SetBold(true);

r1.SetText("日期:

"+weekstart.Replace('-','.')+"—"+weekend.Replace('-','.')+"周次:

"+dt_weekday.Rows[0]["weekindex"].ToString());

r1.SetBold(true);

r1.SetFontFamily("Courier");

r1.SetTextPosition(20);

intdayCnt=0;

//星期对应日期加入hashtable

for(DateTimet=DateTime.Parse(weekstart);t<=DateTime.Parse(weekend);t=t.AddDays

(1))

{

if(dayCnt==7)

break;

hRQtable.Add(t.DayOfWeek.ToString(),t.Date.ToString("yyyy-MM-dd"));

dayCnt=dayCnt+1;

}

}

XWPFTabletable=null;

table=doc.CreateTable();

//table=cteatetemprow(table,dtWeek,dispalyMaterial);

table.Width=1000000;

if(dt.Rows.Count>0)

{

//星期head完毕

//食物与材料不对应分N次循环各餐分七次循环个列

XWPFTableRowm_Row;

XWPFTableCellcell;

CT_Tccttc;

CT_TcPrctPr;

CT_TblWidthwid;

CT_Rctr;

CT_RPrctrpr;

CT_Colorcor;

CT_OnOffb;

CT_HpsMeasuresz;

for(inti=0;i

{

m_Row=table.CreateRow();//创建一行

cell=m_Row.GetCell(0);//m_Row.CreateCell();

cttc=cell.GetCTTc();

ctPr=cttc.AddNewTcPr();

//wid=newCT_TblWidth();

//wid.w="500";

//ctPr.tcW=wid;

ctr=cttc.GetPList()[0].AddNewR();

ctrpr=ctr.AddNewRPr();

ctrpr.AddNewRFonts().hint=ST_Hint.eastAsia;

cor=newCT_Color();

cor.val="black";

ctrpr.color=cor;

b=newCT_OnOff();

b.val=true;

ctrpr.b=b;

sz=newCT_HpsMeasure();

sz.val=(ulong.Parse("25"));

ctrpr.sz=sz;

ctPr.vAlign.val=ST_VerticalJc.center;

ctr.AddNewT().Value=""+dt.Rows[i]["餐次"].ToString()+"";

for(intx=0;x

{

stringweekName="星期"+NumToChina(Convert.ToInt32(dtWeek.Rows[x]["Week"]));

cell=m_Row.CreateCell();

//转化字符串为集合关键字符(:

&)示范(荷包蛋&大米:

10;荷包蛋&鸡蛋:

30)循环li

stringStrMon=dt.Rows[i][weekName].ToString();

stringStrRecipName=null;//菜谱名

stringStrTempRecipName=null;

string[]arr=StrMon.Split(';');

ListmList=newList();

if(StrMon!

="")

{

stringtablefd="";

for(intj=0;j

{

StrTempRecipName=arr[j].Split('&')[0];

if(StrRecipName!

=StrTempRecipName)

{

if(j==0)

tablefd+=arr[j].Split('&')[0];

else

tablefd+="、"+arr[j].Split('&')[0];

}

StrRecipName=arr[j].Split('&')[0];

//组合食材+量集合

mList.Add(arr[j].Split('&')[1]);

}

cell.SetText(tablefd);

}

else

{

if(i==dt.Rows.Count-1)

cell.SetText("..");

}

//显示材料:

Hashtabletb=newHashtable();

if(dispalyMaterial==1)

{

cell=m_Row.CreateCell();

if(mList.Count>0)

{

mList.Sort();

tb=getFoodNameValue(mList);//合并材料

inttempi=1;

foreach(DictionaryEntrydeintb)

{

if(tb.Count!

=tempi)

cell.SetText(de.Key+":

"+de.Value+"g"+"、");//追加==str.append()

else

cell.SetText(de.Key+":

"+de.Value+"g");

tempi=tempi+1;

}

}

else

{

cell.SetText("..");

}

}

}

}

table.RemoveRow(0);

m_Row=table.InsertNewTableRow(0);//创建一行

cell=m_Row.CreateCell();

cttc=cell.GetCTTc();

ctPr=cttc.AddNewTcPr();

wid=newCT_TblWidth();

wid.w="500";

ctPr.tcW=wid;

ctr=cttc.GetPList()[0].AddNewR();

ctrpr=ctr.AddNewRPr();

ctrpr.AddNewRFonts().hint=ST_Hint.eastAsia;

cor=newCT_Color();

cor.val="black";

ctrpr.color=cor;

b=newCT_OnOff();

b.val=true;

ctrpr.b=b;

sz=newCT_HpsMeasure();

sz.val=(ulong.Parse("25"));

ctrpr.sz=sz;

ctr.AddNewT().Value="餐次";

for(inti=0;i

{

stringweekName="星期"+NumToChina(Convert.ToInt32(dtWeek.Rows[i]["Week"]))+"";

cell=m_Row.CreateCell();//创建一个单元格,创建单元格时就创建了一个CT_P

cttc=cell.GetCTTc();//w=newCT_TblWidth();w.w="300";

ctPr=cttc.AddNewTcPr();//ctPr.tcW=w;

ctPr.noWrap.val=true;

ctr=cttc.GetPList()[0].AddNewR();

ctrpr=ctr.AddNewRPr();

ctrpr.b=b;

ctrpr.sz=sz;

if(dispalyMaterial==1)

ctPr.gridSpan.val="2";//合并2列

ctPr.vAlign.val=ST_VerticalJc.both;

ctrpr.AddNewRFonts().hint=ST_Hint.eastAsia;

cttc.GetPList()[0].AddNewPPr().AddNewJc().val=ST_Jc.center;

if(hRQtable[NumToweek(Convert.ToInt32(dtWeek.Rows[i]["Week"])).ToString()]!

=null)

{

weekName+="("+hRQtable[NumToweek(Convert.ToInt32(dtWeek.Rows[i]["Week"]))].ToString()+")";

}

ctr.AddNewT().Value=weekName;

}

}

//table.RemoveRow

(1);

DataTabledt_memo=newDataTable();

CC.Model.R_RecipeWeekMainmodelrwm=newCC.Model.R_RecipeWeekMain();

modelrwm=newCC.DAL.R_RecipeWeekMain().GetModel(recpweekId);

XWPFParagraphp=doc.CreateParagraph();

XWPFRunr=p.CreateRun();

r.SetBold(true);

r.SetText("备注信息:

"+modelrwm.Annotation.ToString());

r.SetBold(true);

r.SetFontFamily("Courier");

p.SetAlignment(ParagraphAlignment.CENTER);

r.SetFontSize(10);

returndoc;

}

#endregion

#region//公共函数

privateDataTableRCC(DataTable_outDataSource)

{

//从DataTable中读取不重复的item行,用来构造新DataTable的列

DataTabledistinct_date=_outDataSource.DefaultView.ToTable(true,"mealName");

DataTablenew_DataTable=newDataTable();

//将名称列添加到新表中

DataColumnnew_mealName_col=newDataColumn();

new_mealName_col.ColumnName="餐次";

new_mealName_col.Caption="";

new_DataTable.Columns.Add(new_mealName_col);

DataColumnnew_mon_col=newDataColumn();

new_mon_col.ColumnName="星期一";

new_mon_col.Caption="";

new_DataTable.Columns.Add(new_mon_col);

DataColumnnew_tue_col=newDataColumn();

new_tue_col.ColumnName="星期二";

new_tue_col.Caption="";

new_DataTable.Columns.Add(new_tue_col);

DataColumnnew_wed_col=newDataColumn();

new_wed_col.ColumnName="星期三";

new_wed_col.Caption="";

new_DataTable.Columns.Add(new_wed_col);

DataColumnnew_thur_col=newDataColumn();

new_thur_col.ColumnName="星期四";

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 职业教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1