C#实现的word转html命令行工具

需求

有个CMS系统的详细信息的来源是人手工编写的word文档,需求如下:

  • 文档是手工编辑而成的word文档(.docx)
  • 文档具备一定的基本格式,其中包括标题、图片、流程图、简介、按序号排布的详细说明
  • 初步只需要将这些文档可以以html页面的形式呈献给用户
  • CMS系统主体结构是基于SpringMVC的

思考

对情况进行了解和思考后,认为可以通过一个中间程序自动化的将word文档转换成为excel,遂决定写个程序来实现这个转换环节。对于这种纯Windows的需求,估计也就是JAVA或者是VS系列语言更加方便一些。最近的项目使用的语言主要是JAVA、VC++,C#还没有尝试过完成实际项目,于是打算用C#尝试实现一下。

流程图(没有难度,主要是练手)

Created with Raphaël 2.1.0 开始 从命令行参数读取 输入的word文档 是否有word文档名输入? 检查文件是否存在 初始化Office组件的 app和doc对象 将文档读入doc对象 将doc对象 另存为html文档 打开并读入刚才 存好的html文档 作一些后续的字符串处理 写回并关闭html文档 结束 yes no yes no

实现代码

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;

namespace word2html
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("说明:本程序用于将word文档转换为html格式文档,支持.doc和.docx格式");
                Console.WriteLine("用法:word2html.exe <待转换的word文档>");
                Console.WriteLine("Copyleft(C)2015 Solomon");
                Console.ReadLine();
                return;
            }

            string srcInputName = args[0]; // 打开文件的位置
            string ext = Path.GetExtension(srcInputName);

            string current_cmd = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string current_dir = Path.GetDirectoryName(current_cmd);
            Console.WriteLine("正在生成html,请稍候...");

            string inputName = current_dir + "\\" + srcInputName;
            string outputName = inputName.Replace(ext, ".html"); // 同路径保存

            if (File.Exists(inputName))
            {

                object oMissing = System.Reflection.Missing.Value;
                object oTrue = true;
                object oFalse = false;

                Word._Application oWord = new Word.Application();
                Word._Document oWordDoc = new Word.Document();

                oWord.Visible = false;
                object openFormat = Word.WdOpenFormat.wdOpenFormatAuto;
                object openName = inputName;

                try
                {
                    oWordDoc = oWord.Documents.Open(ref openName, ref oMissing, ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref openFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                }
                catch (Exception e)
                {
                    Console.WriteLine("读取Word文档时发生异常");
                    oWord.Quit(ref oTrue, ref oMissing, ref oMissing);
                    return;
                }

                object saveFileName = outputName;
                object saveFormat = Word.WdSaveFormat.wdFormatFilteredHTML;

                oWordDoc.SaveAs(ref saveFileName, ref saveFormat, ref oMissing, ref  oMissing, ref oFalse, ref  oMissing, ref oMissing, ref oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing, ref  oMissing);

                oWordDoc.Close(ref oTrue, ref oMissing, ref oMissing);
                oWord.Quit(ref oTrue, ref oMissing, ref oMissing);

                Encoding enc = Encoding.GetEncoding("GB2312");
                string s = File.ReadAllText(outputName, enc);
                s = s.Replace("position:absolute;", "");
                File.WriteAllText(outputName, s, enc);

                Console.WriteLine("Word文档已转换为html格式");
            }
        }
    }
}

后记

1 尤其需要字符集的问题,在windows下使用的汉字兼容字符集事实上是GB2312-80字符集(code page : CP20936)
2 对于“后续的字符串处理”环节,该设计的主要原因是因为页面上因为css绝对定位不准的问题造成了图片内容对于文字内容的遮挡,之后还应该根据实际需求进行进一步的调整

猜你喜欢

转载自blog.csdn.net/solomonlangrui/article/details/47168449
今日推荐