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="星期四";