首页 新闻 赞助 找找看

c# word表格转换成html

0
悬赏园豆:15 [待解决问题]

c# 读取word里的表格,表格是不规制表格,读取出来转换成html文件。

请问大拿们给点提示代码或思想啊。

一生三做的主页 一生三做 | 初学一级 | 园豆:105
提问于:2014-12-02 13:23
< >
分享
所有回答(3)
0

    C#处理word,一般有两种方式:(1)Microsoft.Office.Interop.Word;(2)DocumentFormat.OpenXml,前一种需要安装Office办公套件,启用Word.exe进行处理,后一种是微软提供的开放XML文档处理接口。所以一般采用后者。

    至于如何转换Word表格,我不想单独的处理它,因为需求总是会变化的。你需要知道如果要得到Word其他内容,如何快速的找到解决办法。下面是用DocumentFormat.OpenXml解析Table的一段代码,希望对你有所帮助:

        using (WordprocessingDocument doc = WordprocessingDocument.Open("table.doc", false)) {
                MainDocumentPart mainPart = doc.MainDocumentPart;
                Body body = mainPart.Document.Body;
                foreach (var table in body.Elements<DocumentFormat.OpenXml.Wordprocessing.Table>())
                {
                    foreach (var row in table.Elements<DocumentFormat.OpenXml.Wordprocessing.TableRow>())
                    {
                        foreach (var cell in row.Elements<DocumentFormat.OpenXml.Wordprocessing.TableCell>())
                        {
                            //在这里提取单元格数据封装在Html <table></table>字符串中
                        }
                    }
                }
            }
心梦缘 | 园豆:314 (菜鸟二级) | 2014-12-16 18:16
0

如楼上所说,C#处理Word文档可以调用Microsoft.Office.Interop.Word或DocumentFormat.OpenXml的中的方法,我也觉得用OpenXml比较简单,楼上已经给出。我给出一个用Microsoft.Office.Interop.Word的方法。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Microsoft.Office.Interop.Word;
 7 using Application = Microsoft.Office.Interop.Word.Application;
 8 using System.Reflection;
 9 
10 namespace usingInteropWordProcessTable
11 {
12     class Program
13     {
14         static void Main(string[] args)
15         {
16             var wordApp = new Application();
17             var wordDoc = new Document();
18             var path = @"d:\table.docx";
19             Object oTemplatePath = path;
20             Object oMissing = Missing.Value;
21 
22             /*打开word*/
23             wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
24             Table table = wordDoc.Tables[1];
25             /*这里用索引选择你想选择的表格*/
26             for (int i = 1; i <= wordDoc.Tables[1].Rows.Count; i++)
27             {
28                 for (int j = 1; j <= wordDoc.Tables[1].Rows[i].Cells.Count; j++)
29                 {
30                    string str = wordDoc.Tables[1].Rows[i].Cells[j].Range.Text.ToString();
31                    Console.WriteLine(str);
32                 }
33             }
34 
35             wordDoc.Close();
36             wordApp.Quit();
37 
38             Console.Read();
39         }
40     }
41 }

由此就可以将表格内的数据都读出来,之后就可处理写入到html文件中。

Phineas_姜 | 园豆:202 (菜鸟二级) | 2015-01-06 11:52
0

还可以试试第三方控件哦,我用过Free Spire.doc for .net挺不错的,支持将word转换为html。产品下载链接:https://www.e-iceblue.cn/Downloads/Free-Spire-Doc-NET.html
参考代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace ConvertToHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void button1_Click(object sender, EventArgs e)
    {
        //Create word document
        Document document = new Document();
        document.LoadFromFile(@"..\..\..\..\..\..\..\Data\ToHtmlTemplate.docx");

        //Save doc file.
        document.SaveToFile("Sample.html", FileFormat.Html);

        //Launching the MS Word file.
        WordDocViewer("Sample.html");
    }

    private void WordDocViewer(string fileName)
    {
        try
        {
            System.Diagnostics.Process.Start(fileName);
        }
        catch { }
    }
}

}

Tina_Tang | 园豆:346 (菜鸟二级) | 2021-07-19 16:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册