首页 新闻 会员 周边

JAVA根据程序中的数据源(可能是一段HTML或者是一些实体类)生成Word和HTML

0
悬赏园豆:100 [已解决问题] 解决于 2016-04-22 20:05

我用了FreeMaker导出了Word但是因为模板是xml格式的,所以Word本质上也是XML,这种情况怎么转换为HTML呢?

问题补充:

我的html页面重要展示柱状图、折线图之类的图表

小菜变大鸟的主页 小菜变大鸟 | 初学一级 | 园豆:5
提问于:2016-04-22 11:07
< >
分享
最佳答案
0

你可以考虑下Web端的图标组件

收获园豆:100
千亦千寻 | 菜鸟二级 |园豆:404 | 2016-04-22 20:04
其他回答(3)
0

我有一段java程序,处理word上传后转换为html格式显示的,可是我不懂java。

waiter | 园豆:1000 (小虾三级) | 2016-04-22 13:07
1

你的意思是基于模板引擎做事吧 

我这今天也在弄这个 

html 完全可以找razor这个级别的模板引擎 

而word可以根据html去生成 这是我这几天折腾的代码 希望可以给你代码帮助 

 private static byte[] HtmlIntoWord(string htmlText, string wordPath)
        {
            string filePath = FilePath + DateTime.Now.Ticks + ".html";
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            using (FileStream fileStream = File.Create(filePath))
            {
                byte[] htmlBytes = System.Text.Encoding.Default.GetBytes(htmlText);
                fileStream.Write(htmlBytes, 0, htmlBytes.Length);
                fileStream.Dispose();
            }


            System.Windows.Forms.WebBrowser WB = new System.Windows.Forms.WebBrowser();//新建内置浏览
            WB.Navigate(filePath);//加载页面
            //加载完成
            while (WB.ReadyState != WebBrowserReadyState.Complete)
            {
                System.Windows.Forms.Application.DoEvents();
            }
            //对加载完成的页面进行全选和复制操作
            HtmlDocument doc = WB.Document;
            doc.ExecCommand("SelectAll", false, "");//全选
            doc.ExecCommand("Copy", false, "");//复制
            //放入剪切板
            System.Windows.Forms.IDataObject iData = Clipboard.GetDataObject();
            SaveWord(wordPath);//保存为word文档
            //读取文档,下载文档
            FileStream fs = new FileStream(wordPath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            File.Delete(filePath);
            return bytes;

        }
        public static void SaveWord(string path = "")
        {
            object currentPath;
            if (path == "")
            {
                currentPath = FilePath + "test.doc";          //设置文件保存路劲
            }
            else
            {
                currentPath = path;
            }
            //object path;                      //声明文件路径变量
            //string wordstr = wdstr;                   //声明word文档内容
            Microsoft.Office.Interop.Word.Application wordApp;       //声明word应用程序变量
            Microsoft.Office.Interop.Word.Document worddoc;          //声明word文档变量    

            //初始化变量
            object Nothing = Missing.Value;                       //COM调用时用于占位
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument; //Word文档的保存格式
            wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();              //声明一个wordAPP对象
            worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing,
                ref Nothing, ref Nothing);

            //页面设置
            worddoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
            worddoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向


            //向文档中写入内容(直接粘贴)
            worddoc.Paragraphs.Last.Range.Paste();

            //保存文档

            worddoc.SaveAs(ref currentPath, ref format, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                ref Nothing, ref Nothing, ref Nothing, ref Nothing);

            //关闭文档
            worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //关闭worddoc文档对象
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //关闭wordApp组对象
        }

 

 

小眼睛老鼠 | 园豆:2731 (老鸟四级) | 2016-04-22 13:18
0

不采用这种方式了,我自己再想想

小菜变大鸟 | 园豆:5 (初学一级) | 2016-04-22 20:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册