Пример #1
0
        private bool WordToPDF(string inePath, string outputPath)
        {
            bool result = false;

            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Word.Document document = null;
            try
            {
                application.Visible = false;
                document            = application.Documents.Open(inePath);
                document.ExportAsFixedFormat(outputPath, Word.WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
                application.Quit();
            }
            return(result);
        }
Пример #2
0
        public void ExportDataGridViewToWord(DataTable srcDgv)
        {
            if (srcDgv.Rows.Count == 0)
            {
                Page.RegisterStartupScript("alt", "<script>alert('没有数据可供导出!')</script>");
                return;
            }
            else
            {
                Object none = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application wordApp  = new Microsoft.Office.Interop.Word.Application();
                Microsoft.Office.Interop.Word.Document    document = wordApp.Documents.Add(ref none, ref none, ref none, ref none);
                //建立表格
                Microsoft.Office.Interop.Word.Table table = document.Tables.Add(document.Paragraphs.Last.Range, srcDgv.Rows.Count + 1, srcDgv.Columns.Count, ref none, ref none);

                try
                {
                    table.Cell(1, 1).Range.Text  = "姓名";
                    table.Cell(1, 2).Range.Text  = "性别";
                    table.Cell(1, 3).Range.Text  = "年龄";
                    table.Cell(1, 4).Range.Text  = "学历";
                    table.Cell(1, 5).Range.Text  = "手机";
                    table.Cell(1, 6).Range.Text  = "电子邮件";
                    table.Cell(1, 7).Range.Text  = "英语等级";
                    table.Cell(1, 8).Range.Text  = "求职意向";
                    table.Cell(1, 9).Range.Text  = "工作地点";
                    table.Cell(1, 10).Range.Text = "工作年限";
                    table.Cell(1, 11).Range.Text = "期望薪水";
                    table.Cell(1, 12).Range.Text = "毕业院校";
                    table.Cell(1, 13).Range.Text = "专业";
                    table.Cell(1, 14).Range.Text = "最近单位";
                    table.Cell(1, 15).Range.Text = "最近职位";
                    table.Cell(1, 16).Range.Text = "接收时间";
                    //输出控件中的记录
                    for (int i = 0; i < srcDgv.Rows.Count; i++)
                    {
                        for (int j = 0; j < srcDgv.Columns.Count; j++)
                        {
                            table.Cell(i + 2, j + 1).Range.Text = srcDgv.Rows[i][j].ToString();
                        }
                    }
                    table.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                    table.Borders.InsideLineStyle  = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                    string newFile = Server.MapPath("~/wordInfo/") + DateTime.Now.ToString("yyyyMMddHHmmssss") + ".doc";
                    document.SaveAs(newFile, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none, ref none);

                    Page.RegisterStartupScript("alt", "<script>alert('数据成功导出!')</script>");
                }
                catch (Exception e)
                {
                    Page.RegisterStartupScript("alt", "<script>alert('" + e.Message + "')</script>");
                }
            }
        }
Пример #3
0
        /// <summary>
        /// word转换html
        /// </summary>
        /// <param name="strFile">全路径</param>
        /// <returns></returns>
        private string GetPathByDocToHTML(string strFile)
        {
            try
            {
                if (string.IsNullOrEmpty(strFile))
                {
                    return("0"); //没有文件
                }

                Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
                Type wordType = word.GetType();
                Microsoft.Office.Interop.Word.Documents docs = word.Documents;

                // 打开文件
                Type docsType = docs.GetType();

                object fileName = strFile;

                Microsoft.Office.Interop.Word.Document doc =
                    (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                                                                                  System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

                // 转换格式,另存为html
                Type docType = doc.GetType();
                //给文件重新起名
                string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() +
                                  System.DateTime.Now.Day.ToString() +
                                  System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() +
                                  System.DateTime.Now.Second.ToString();

                string   strFileFolder = "~/html/";
                DateTime dt            = DateTime.Now;
                //以yyyymmdd形式生成子文件夹名
                string strFileSubFolder = dt.Year.ToString();
                strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString();
                strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString();
                string strFilePath = strFileFolder + strFileSubFolder + "/";
                // 判断指定目录下是否存在文件夹,如果不存在,则创建
                if (!Directory.Exists(Server.MapPath(strFilePath)))
                {
                    // 创建up文件夹
                    Directory.CreateDirectory(Server.MapPath(strFilePath));
                }

                //被转换的html文档保存的位置
                // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
                string ConfigPath   = Server.MapPath(strFilePath + filename + ".html");
                object saveFileName = ConfigPath;

                /*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
                 * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                 * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
                 * 其它格式:
                 * wdFormatHTML
                 * wdFormatDocument
                 * wdFormatDOSText
                 * wdFormatDOSTextLineBreaks
                 * wdFormatEncodedText
                 * wdFormatRTF
                 * wdFormatTemplate
                 * wdFormatText
                 * wdFormatTextLineBreaks
                 * wdFormatUnicodeText
                 */
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                                     null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

                //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
                //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });

                //关闭文档
                docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
                                     null, doc, new object[] { null, null, null });

                // 退出 Word
                wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
                //转到新生成的页面
                //return ("/" + filename + ".html");

                //转化HTML页面统一编码格式
                TransHTMLEncoding(ConfigPath);

                File.Delete(strFile); //删除临时保存的文件

                return(strFilePath + filename + ".html");
            }
            catch (Exception ex)
            {
                using (FileStream fs = new FileStream(Server.MapPath("~/log/") + DateTime.Now.ToString("yyyyMMdd") + ".txt", FileMode.OpenOrCreate, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine(ex.ToString() + "\n" + ex.Message.ToString() + "\n" + ex.StackTrace + "\n" + ex.InnerException + "\n");
                    };
                };

                File.Delete(strFile); //删除临时保存的文件
                Page.RegisterStartupScript("alt", "<script>alert('转Html出错了。')</script>");
                return("0");          //没有文件
            }
        }