如何將html轉為word(怎樣把html檔案轉換成word)

做開發的都知道,在專案開發中要操作匯出word都挺麻煩的,大家都儘量選擇使用開源的工具。

小編簡單分析了下,大家現在整理三種方法供大家參考:

一、使用jquery.wordexport.js

首先引用三個檔案:jquery.wordexport.js依賴於jquery以及 FileSaver.js

呼叫方法:

我是要匯出word的html內容

是不是超級簡單。。。。。

二、使用DotNetCore.NPOI, 直接操作生成 word

首先在專案中新增nuget DotNetCore.NPOI

提供一個封裝的類:NpoiWordHelper.cs

using NPOI.OpenXmlFormats.Wordprocessing;

using NPOI.XWPF.UserModel;

using System;

using System.IO;

namespace TianFeng.FrameworkCore

{

///

/// NPOI操作Word

///

public class NpoiWordHelper

{

///

/// 建立文件

///

///

public static void ExportDocument(DocumentSetting setting)

{

XWPFDocument docx = new XWPFDocument();

MemoryStream ms = new MemoryStream();

//設定文件

docx.Document.body.sectPr = new CT_SectPr();

CT_SectPr setPr = docx.Document.body.sectPr;

//獲取頁面大小

Tuplesize = GetPaperSize(setting.PaperType);

setPr.pgSz.w = (ulong)size.Item1;

setPr.pgSz.h = (ulong)size.Item2;

//建立一個段落

CT_P p = docx.Document.body.AddNewP();

//段落水平居中

p.AddNewPPr().AddNewJc().val = ST_Jc.center;

XWPFParagraph gp = new XWPFParagraph(p, docx);

XWPFRun gr = gp.CreateRun();

//建立標題

if (!string.IsNullOrEmpty(setting.TitleSetting.Title))

{

gr.GetCTR().AddNewRPr().AddNewRFonts().ascii = setting.TitleSetting.FontName;

gr.GetCTR().AddNewRPr().AddNewRFonts().eastAsia = setting.TitleSetting.FontName;

gr.GetCTR().AddNewRPr().AddNewRFonts().hint = ST_Hint.eastAsia;

gr.GetCTR().AddNewRPr().AddNewSz().val = (ulong)
   setting.TitleSetting.FontSize;//2號字型

gr.GetCTR().AddNewRPr().AddNewSzCs().val = (ulong)setting.TitleSetting.FontSize;

gr.GetCTR().AddNewRPr().AddNewB().val =
   setting.TitleSetting.HasBold; //加粗

gr.GetCTR().AddNewRPr().AddNewColor().val = "black";//字型顏色

gr.SetText(setting.TitleSetting.Title);

}

//建立文件主要內容

if (!string.IsNullOrEmpty(setting.MainContentSetting.MainContent))

{

p = docx.Document.body.AddNewP();

p.AddNewPPr().AddNewJc().val = ST_Jc.both;

gp = new XWPFParagraph(p, docx)

{

IndentationFirstLine = 2

};

//單倍為預設值(240)不需設定,1.5倍=240X1.5=360,2倍=240X2=480

p.AddNewPPr().AddNewSpacing().line = "400";//固定20磅

p.AddNewPPr().AddNewSpacing().lineRule = ST_LineSpacingRule.exact;

gr = gp.CreateRun();

CT_RPr rpr = gr.GetCTR().AddNewRPr();

CT_Fonts rfonts = rpr.AddNewRFonts();

rfonts.ascii = setting.MainContentSetting.FontName;

rfonts.eastAsia = setting.MainContentSetting.FontName;

rpr.AddNewSz().val = (ulong)
   setting.MainContentSetting.FontSize;//5號字型-21

rpr.AddNewSzCs().val = (ulong)setting.MainContentSetting.FontSize;

rpr.AddNewB().val = setting.MainContentSetting.HasBold;

gr.SetText(setting.MainContentSetting.MainContent);

//// 建立表格物件列數寫死了,可根據自己需要改進或者自己想想解決方案

//XWPFTable table = docx.CreateTable(list.Count(), 4);

//for (int i = 0; i < list.Count(); i )

//{

// table.GetRow(i).GetCell(0).SetText(list[i].Id.ToString());

// table.GetRow(i).GetCell(1).SetText(list[i].Title);

// table.GetRow(i).GetCell(2).SetText(list[i].Content);

// table.GetRow(i).GetCell(3).SetText(list[i].AddTime);

//}

}

//開始寫入

docx.Write(ms);

using (FileStream fs = new FileStream(setting.SavePath, FileMode.Create, FileAccess.Write))

{

byte[] data = ms.ToArray();

fs.Write(data, 0, data.Length);

fs.Flush();

}

ms.Close();

}

///

/// 設定文件

///

public class DocumentSetting

{

///

/// 文件型別,預設為A4縱向

///

public PaperType PaperType { get; set; } = PaperType.A4_V;

///

/// 儲存地址,包含檔名

///

public string SavePath { get; set; }

///

/// 文件標題相關

///

public ContentItemSetting TitleSetting { get; set; }

///

/// 文件主要內容

///

public ContentItemSetting MainContentSetting { get; set; }

}

///

/// 文件內容相關

///

public class ContentItemSetting

{

///

/// 標題

///

public string Title { get; set; }

///

/// 主要內容

///

public string MainContent { get; set; }

///

/// 使用字型

///

public string FontName { get; set; } = "宋體";

///

/// 字型大小,預設2號字型

///

public int FontSize { get; set; } = 44;

///

/// 是否加粗,預設不加粗

///

public bool HasBold { get; set; } = false;

}

///

/// 紙張型別

///

public enum PaperType

{

///

/// A4縱向

///

A4_V,

///

/// A4橫向

///

A4_H,

///

/// A5縱向

///

A5_V,

///

/// A5橫向

///

A5_H,

///

/// A6縱向

///

A6_V,

///

/// A6橫向

///

A6_H

}

#region 私有方法

///

/// 獲取紙張大小,單位:Twip

///

換算關係:1英寸=1440緹 1釐米=567緹 1磅=20緹 1畫素=15緹

///

///

///

private static TupleGetPaperSize(PaperType type)

{

Tupleres = null;

switch (type)

{

case PaperType.A4_V:

res = new Tuple(11906, 16838);

break;

case PaperType.A4_H:

res = new Tuple(16838, 11906);

break;

case PaperType.A5_V:

res = new Tuple(8390, 11906);

break;

case PaperType.A5_H:

res = new Tuple(11906, 8390);

break;

case PaperType.A6_V:

res = new Tuple(5953, 8390);

break;

case PaperType.A6_H:

res = new Tuple(8390, 5953);

break;

}

return res;

}

#endregion

}

}

呼叫方法:

NpoiWordHelper.ExportDocument(

new DocumentSetting

{

PaperType = PaperType.A4_V,

SavePath="word標題.docx",

TitleSetting=new ContentItemSetting

{

Title="我的word標題",

FontSize=44,

HasBold=true,

FontName="宋體"

},

MainContentSetting = new ContentItemSetting

{

MainContent= "這裡是word內容這裡是word內容這裡是word內容這裡是word內容這裡是word內容",

FontSize = 14,

FontName = "宋體"

}

}

);

是不是一樣很簡單,但有個遺憾,這個封裝的類不支援插入表格、圖片,需要的還請大家自行擴充套件。

三、使用aspose.words

這個是第三方的,未開源,要有license不然會有水印,巧的是在nuget上發一個破解的。。大家偷偷用就好。

呼叫方法:

Aspose.Words.Document doc = new Aspose.Words.Document();

Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.InsertHtml("html內容");

doc.Save("word.docx", Aspose.Words.SaveFormat.Docx);

這個使用應該來說是最後簡單,強烈推薦,Aspose.Words同時可以進行轉pdf,等檔案的互轉,功能非常強大。其他功能,大家自行研究吧。