You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
176 lines
6.8 KiB
176 lines
6.8 KiB
9 months ago
|
|
||
|
using NPOI.OpenXmlFormats.Wordprocessing;
|
||
|
using NPOI.XWPF.UserModel;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Data;
|
||
|
using System.Web;
|
||
|
|
||
|
namespace FangYar.Common
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// NPOIWordHelper 的摘要说明
|
||
|
/// </summary>
|
||
|
public class NPOIWordHelper
|
||
|
{
|
||
|
|
||
|
/// <summary>
|
||
|
/// 导出word
|
||
|
/// </summary>
|
||
|
/// <param name="sender"></param>
|
||
|
/// <param name="e"></param>
|
||
|
public static void ExportByWeb(DataTable dtSource, string HeaderText, Dictionary<string, string> cellHeard, string filename)
|
||
|
{
|
||
|
//创建document对象
|
||
|
XWPFDocument doc = new XWPFDocument();
|
||
|
//创建段落对象
|
||
|
XWPFParagraph p1 = doc.CreateParagraph();
|
||
|
p1.Alignment = ParagraphAlignment.CENTER;//字体居中
|
||
|
|
||
|
XWPFRun runTitle = p1.CreateRun();
|
||
|
runTitle.IsBold = true;
|
||
|
runTitle.SetText("" + HeaderText);
|
||
|
runTitle.FontSize = 24;
|
||
|
runTitle.SetFontFamily("宋体", FontCharRange.None);//设置雅黑字体
|
||
|
|
||
|
//XWPFParagraph p2 = doc.CreateParagraph();
|
||
|
//XWPFRun run1 = p2.CreateRun();
|
||
|
//run1.SetText(" 军检项目号:");
|
||
|
//run1.FontSize = 12;
|
||
|
//run1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
|
||
|
|
||
|
|
||
|
#region 检验要素列表部分(数据库读取循环显示)
|
||
|
|
||
|
/* 打印1页:小于8行数据,创建9行;
|
||
|
* 打印2页:大于8小于26行数据,创建27行。增加18
|
||
|
* 打印3页:大于26小于44行数据,创建45行。增加18
|
||
|
*/
|
||
|
int columns = dtSource.Columns.Count;
|
||
|
int rows = dtSource.Rows.Count + 1;
|
||
|
XWPFTable tableContent = doc.CreateTable(rows, columns);
|
||
|
tableContent.Width = 1000 * columns;
|
||
|
tableContent.Width = 1000 * 5;
|
||
|
foreach (DataColumn column in dtSource.Columns)
|
||
|
{
|
||
|
string ColumnName = column.ColumnName;
|
||
|
tableContent.SetColumnWidth(column.Ordinal, 1000);/* 设置列宽 */
|
||
|
try
|
||
|
{
|
||
|
if (cellHeard != null && cellHeard[ColumnName] != "")
|
||
|
{
|
||
|
|
||
|
ColumnName = cellHeard[ColumnName];
|
||
|
}
|
||
|
}
|
||
|
catch { }
|
||
|
tableContent.GetRow(0).GetCell(column.Ordinal).SetParagraph(SetCellText(doc, tableContent, ColumnName));
|
||
|
}
|
||
|
for (int i = 0; i < dtSource.Rows.Count; i++)
|
||
|
{
|
||
|
for (int n = 0; n < dtSource.Columns.Count; n++)
|
||
|
{
|
||
|
tableContent.GetRow(i + 1).GetCell(n).SetParagraph(SetCellText(doc, tableContent, dtSource.Rows[i][n].ToString(), ParagraphAlignment.CENTER, 50));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
|
||
|
//保存文件到磁盘WinForm
|
||
|
//string docPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DocxWord");
|
||
|
|
||
|
//if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); }
|
||
|
|
||
|
|
||
|
//string fileName = string.Format("{0}.doc", HttpUtility.UrlEncode("jjysd" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8));
|
||
|
|
||
|
//FileStream out1 = new FileStream(Path.Combine(docPath, fileName), FileMode.Create);
|
||
|
//doc.Write(out1);
|
||
|
//out1.Close();
|
||
|
|
||
|
#region 保存导出WebForm
|
||
|
|
||
|
//Response.Redirect(ResolveUrl(string.Format(@"~\DocxWord\{0}", fileName)));
|
||
|
HttpContext curContext = HttpContext.Current;
|
||
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||
|
doc.Write(ms);
|
||
|
curContext.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", HttpUtility.UrlEncode(filename + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8)));
|
||
|
curContext.Response.BinaryWrite(ms.ToArray());
|
||
|
curContext.Response.End();
|
||
|
|
||
|
ms.Close();
|
||
|
ms.Dispose();
|
||
|
|
||
|
//using (MemoryStream ms = new MemoryStream())
|
||
|
//{
|
||
|
// doc.Write(ms);
|
||
|
// Response.ClearContent();
|
||
|
// Response.Buffer = true;
|
||
|
// Response.ContentType = "application/octet-stream";
|
||
|
// Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.doc", HttpUtility.UrlEncode("军检验收单" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff"), System.Text.Encoding.UTF8)));
|
||
|
// Response.BinaryWrite(ms.ToArray());
|
||
|
// //Response.End();
|
||
|
// Response.Flush();
|
||
|
// doc = null;
|
||
|
// ms.Close();
|
||
|
// ms.Dispose();
|
||
|
//}
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 设置字体格式
|
||
|
/// </summary>
|
||
|
/// <param name="doc"></param>
|
||
|
/// <param name="table"></param>
|
||
|
/// <param name="setText"></param>
|
||
|
/// <returns></returns>
|
||
|
public static XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
|
||
|
{
|
||
|
//table中的文字格式设置
|
||
|
CT_P para = new CT_P();
|
||
|
XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
|
||
|
pCell.Alignment = ParagraphAlignment.CENTER;//字体居中
|
||
|
pCell.VerticalAlignment = TextAlignment.CENTER;//字体居中
|
||
|
XWPFRun r1c1 = pCell.CreateRun();
|
||
|
r1c1.SetText(setText);
|
||
|
r1c1.FontSize = 12;
|
||
|
r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
|
||
|
//r1c1.SetTextPosition(20);//设置高度
|
||
|
|
||
|
return pCell;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 设置单元格格式
|
||
|
/// </summary>
|
||
|
/// <param name="doc">doc对象</param>
|
||
|
/// <param name="table">表格对象</param>
|
||
|
/// <param name="setText">要填充的文字</param>
|
||
|
/// <param name="align">文字对齐方式</param>
|
||
|
/// <param name="textPos">rows行的高度</param>
|
||
|
/// <returns></returns>
|
||
|
public static XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText, ParagraphAlignment align, int textPos)
|
||
|
{
|
||
|
CT_P para = new CT_P();
|
||
|
XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
|
||
|
//pCell.Alignment = ParagraphAlignment.LEFT;//字体
|
||
|
pCell.Alignment = align;
|
||
|
|
||
|
XWPFRun r1c1 = pCell.CreateRun();
|
||
|
r1c1.SetText(setText);
|
||
|
r1c1.FontSize = 12;
|
||
|
r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体
|
||
|
//r1c1.SetTextPosition(textPos);//设置高度
|
||
|
r1c1.TextPosition = textPos;
|
||
|
|
||
|
return pCell;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|