1、Npoi205导出word合并行和合并列using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Text;using System.Data;using System.Collections;using System.IO;using NPOI;using NPOI.HSSF.UserModel;
2、using DAL = CC.DAL;using CC.Common;using BLL = CC.BLL;using NPOI.XWPF.UserModel;using NPOI.OpenXml4Net;using NPOI.OpenXmlFormats.Wordprocessing;/* * 功能:食谱分析报告导出 * 编写人: * 日期:2014.5.27 * nopi版本:2.0.5.0 */public partial class ExportFoodCheckList : BasePage #region /加载模板 protected void Page_Load(object
3、sender, EventArgs e) if (RequestRecweekId != null) string type = Requesttype.ToString(); int RecweekId = int.Parse(RequestRecweekId.ToString(); ViewStateHidRecipwweekId = RecweekId.ToString(); if (type = Rec) XWPFDocument doc = HtmlExport(Int32.Parse(ViewStateHidRecipwweekId.ToString(), 0);/.Replace
4、(table border=0 class=table_food mt10, table border=1 );/this.DivContent.InnerHtml.ToString().Replace(table border=0 class=table_food mt10, 0) /设置 顶部title weekstart = dt_weekday.Rows0weekstart.ToString(); weekend = dt_weekday.Rows0weekend.ToString(); /设置 顶部title XWPFParagraph p1 = doc.CreateParagrap
5、h(); XWPFRun r1 = p1.CreateRun(); r1.SetBold(true); r1.SetText(dt_weekday.Rows0name.ToString() + strtitle); r1.SetBold(true); r1.SetFontFamily(Courier); p1.SetAlignment(ParagraphAlignment.CENTER); r1.SetFontSize(25); p1 = doc.CreateParagraph(); p1.SetAlignment(ParagraphAlignment.CENTER); r1.SetFontS
6、ize(15); r1 = p1.CreateRun(); r1.SetBold(true); r1.SetText(日期: + weekstart.Replace(-, .) + + weekend.Replace(-, .) + 周次: + dt_weekday.Rows0weekindex.ToString(); r1.SetBold(true); r1.SetFontFamily(Courier); r1.SetTextPosition(20); int dayCnt = 0; /星期对应日期加入hashtable for (DateTime t = DateTime.Parse(we
7、ekstart); t 0) /星期head 完毕 /食物与材料不对应 分N次循环各餐 分七次循环个列 XWPFTableRow m_Row; XWPFTableCell cell; CT_Tc cttc; CT_TcPr ctPr; CT_TblWidth wid; CT_R ctr; CT_RPr ctrpr; CT_Color cor; CT_OnOff b; CT_HpsMeasure sz; for (int i = 0; i dt.Rows.Count; i+) /循环餐 m_Row = table.CreateRow();/创建一行 cell = m_Row.GetCell(0)
8、;/ m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); /wid = new CT_TblWidth(); /wid.w = 500; /ctPr.tcW = wid; ctr = cttc.GetPList()0.AddNewR(); ctrpr = ctr.AddNewRPr(); ctrpr.AddNewRFonts().hint = ST_Hint.eastAsia; cor = new CT_Color(); cor.val = black; ctrpr.color = cor; b = new
9、CT_OnOff(); b.val = true; ctrpr.b = b; sz = new CT_HpsMeasure(); sz.val = (ulong.Parse(25); ctrpr.sz = sz; ctPr.vAlign.val = ST_VerticalJc.center; ctr.AddNewT().Value = + dt.Rowsi餐次.ToString() + ; for (int x = 0; x dtWeek.Rows.Count; x+) /循环列 string weekName = 星期 + NumToChina(Convert.ToInt32(dtWeek.
10、RowsxWeek); cell = m_Row.CreateCell(); / 转化字符串为集合 关键字符(:&)示范(荷包蛋&大米:10;荷包蛋&鸡蛋:30 )循环li string StrMon = dt.RowsiweekName.ToString(); string StrRecipName = null; /菜谱名 string StrTempRecipName = null; string arr = StrMon.Split(;); List mList = new List(); if (StrMon != ) string tablefd = ; for (int j =
11、0; j 0) mList.Sort(); tb = getFoodNameValue(mList); /合并 材料 int tempi = 1; foreach (DictionaryEntry de in tb) 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(
12、0); m_Row = table.InsertNewTableRow(0);/创建一行 cell = m_Row.CreateCell(); cttc = cell.GetCTTc(); ctPr = cttc.AddNewTcPr(); wid = new CT_TblWidth(); wid.w = 500; ctPr.tcW = wid; ctr = cttc.GetPList()0.AddNewR(); ctrpr = ctr.AddNewRPr(); ctrpr.AddNewRFonts().hint = ST_Hint.eastAsia; cor = new CT_Color()
13、; cor.val = black; ctrpr.color = cor; b = new CT_OnOff(); b.val = true; ctrpr.b = b; sz = new CT_HpsMeasure(); sz.val = (ulong.Parse(25); ctrpr.sz = sz; ctr.AddNewT().Value = 餐次 ; for (int i = 0; i dtWeek.Rows.Count; i+) string weekName = 星期 + NumToChina(Convert.ToInt32(dtWeek.RowsiWeek) + ; cell =
14、m_Row.CreateCell();/创建一个单元格,创建单元格时就创建了一个CT_P cttc = cell.GetCTTc(); / w = new CT_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 =
15、 2;/合并2列 ctPr.vAlign.val = ST_VerticalJc.both; ctrpr.AddNewRFonts().hint = ST_Hint.eastAsia; cttc.GetPList()0.AddNewPPr().AddNewJc().val = ST_Jc.center; if (hRQtableNumToweek(Convert.ToInt32(dtWeek.RowsiWeek).ToString() != null) weekName += ( + hRQtableNumToweek(Convert.ToInt32(dtWeek.RowsiWeek).ToS
16、tring() + ); ctr.AddNewT().Value = weekName; / table.RemoveRow(1); DataTable dt_memo = new DataTable(); CC.Model.R_RecipeWeekMain modelrwm = new CC.Model.R_RecipeWeekMain(); modelrwm = new CC.DAL.R_RecipeWeekMain().GetModel(recpweekId); XWPFParagraph p = doc.CreateParagraph(); XWPFRun r = p.CreateRu
17、n(); r.SetBold(true); r.SetText(备注信息: + modelrwm.Annotation.ToString(); r.SetBold(true); r.SetFontFamily(Courier); p.SetAlignment(ParagraphAlignment.CENTER); r.SetFontSize(10); return doc; #endregion #region /公共函数 private DataTable RCC(DataTable _outDataSource) /从DataTable中读取不重复的item行,用来构造新DataTable
18、的列 DataTable distinct_date = _outDataSource.DefaultView.ToTable(true, mealName); DataTable new_DataTable = new DataTable(); /将名称列添加到新表中 DataColumn new_mealName_col = new DataColumn(); new_mealName_col.ColumnName = 餐次; new_mealName_col.Caption = ; new_DataTable.Columns.Add(new_mealName_col); DataColu
19、mn new_mon_col = new DataColumn(); new_mon_col.ColumnName = 星期一; new_mon_col.Caption = ; new_DataTable.Columns.Add(new_mon_col); DataColumn new_tue_col = new DataColumn(); new_tue_col.ColumnName = 星期二; new_tue_col.Caption = ; new_DataTable.Columns.Add(new_tue_col); DataColumn new_wed_col = new DataColumn(); new_wed_col.ColumnName = 星期三; new_wed_col.Caption = ; new_DataTable.Columns.Add(new_wed_col); DataColumn new_thur_col = new DataColumn(); new_thur_col.ColumnName = 星期四;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1