var filename = "餐桌二维码信息_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".doc";
string path = Server.MapPath("~/Templates/Temp/" + filename);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//清除设置
builder.PageSetup.ClearFormatting();
builder.StartTable();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //标题居中
builder.Bold = true;//字体加粗
builder.Font.Size = 16; //字体大小
builder.RowFormat.Height = 30;
builder.InsertCell();//插入Table单元格
builder.CellFormat.Width = 400;
builder.CellFormat.Borders.LineStyle = LineStyle.Hairline;
builder.CellFormat.Borders.Color = Color.White;
builder.Writeln("测试导入word");
builder.EndRow();
//插入图片
List<string> list = new List<string>{"0.jpg","1.jpg", "2.jpg", "3.jpg"};
foreach(var item in list)
{
var bt = GetIMGbyte(item);
Insert(builder,bt,100,100,"测试插入",true);
}
builder.EndTable();
doc.Save(path);
return View();
private void Insert(DocumentBuilder builder, byte[] bt = null, int width = 0, int height = 0, string str = "", bool isimg = true)
{
//设置表格行高度
builder.RowFormat.Height = height;
//插入Table单元格
builder.InsertCell();
//Table单元格边框线样式
builder.CellFormat.Borders.LineStyle = LineStyle.Hairline;
builder.CellFormat.Borders.Color = Color.White;
//设置单元格宽度
builder.CellFormat.Width = width;
//此单元格中内容垂直对齐方式
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.CellFormat.VerticalMerge = CellMerge.None;
//此单元格中内容水平对齐方式
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//字体大小
builder.Font.Size = 12;
//是否加粗
builder.Bold = true;
//向此单元格中添加内容
if (isimg)
{
builder.InsertImage(bt, width, height);
builder.Writeln(" ");
}
builder.Writeln(str);
}
private byte[] GetIMGbyte(string PicturePath)
{
var imgPath = Server.MapPath("~/Content/Images/" + PicturePath);
//将需要存储的图片读取为数据流
FileStream fs = new FileStream(imgPath, FileMode.Open, FileAccess.Read);
Byte[] btye2 = new byte[fs.Length];
fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
fs.Close();
return btye2;
}