zoukankan      html  css  js  c++  java
  • WordReportDocX

    public static void ReplaceWithImage(DocX document, string tagBody, string replacement, int width, int height)
    {

    foreach (var paragraph in document.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    Image img = document.AddImage(replacement);
    var picture = img.CreatePicture(height, width);
    paragraph.InsertPicture(picture, index + 1);
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }

    Header oddHeader = document.Headers.odd;
    if (oddHeader != null)
    {
    foreach (var paragraph in oddHeader.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    Image img = document.AddImage(replacement);
    var picture = img.CreatePicture(height, width);
    paragraph.InsertPicture(picture, index + 1);
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }
    }
    Header evenHeader = document.Headers.even;
    if (evenHeader != null)
    {
    foreach (var paragraph in evenHeader.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    Image img = document.AddImage(replacement);
    var picture = img.CreatePicture(height, width);
    paragraph.InsertPicture(picture, index + 1);
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }
    }


    Footer oddFooter = document.Footers.odd;
    if (oddFooter != null)
    {
    foreach (var paragraph in oddFooter.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    Image img = document.AddImage(replacement);
    var picture = img.CreatePicture(height, width);
    paragraph.InsertPicture(picture, index + 1);
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }
    }

    Footer evenFooter = document.Footers.even;
    if (evenFooter != null)
    {
    foreach (var paragraph in evenFooter.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    Image img = document.AddImage(replacement);
    var picture = img.CreatePicture(height, width);
    paragraph.InsertPicture(picture, index + 1);
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }
    }
    }

    public static Table ReplaceWithTable(DocX document, string tagBody, int row, int col)
    {
    Table tb = null;
    foreach (var paragraph in document.Paragraphs)
    {
    paragraph.FindAll(tagBody).ForEach(index =>
    {
    paragraph.RemoveText(index, tagBody.Length - 1);
    try
    {
    tb = paragraph.InsertTableAfterSelf(document.AddTable(row, col));
    }
    catch
    {
    }
    paragraph.RemoveText(index, 1);
    });
    }
    return tb;

    }
    //通过模板创建新文档
    public static DocX CreateTemplateDocument(string filePath, string TemplateFile)
    {
    DocX wordDoc = DocX.Create(filePath, DocumentTypes.Document);
    try
    {
    using (FileStream file = new FileStream(TemplateFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
    wordDoc.ApplyTemplate(file);
    file.Close();
    return wordDoc;
    }
    }
    catch
    {
    return null;
    }
    }
    //通过模板创建新文档
    public static DocX LoadTemplateDocument(string TemplateFile)
    {

    try
    {
    using (FileStream file = new FileStream(TemplateFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
    DocX wordDoc = DocX.Load(file);
    file.Close();
    return wordDoc;
    }
    }
    catch
    {
    return null;
    }
    }
    //通过模板创建新文档
    public static DocX CreateDocument(string filePath)
    {
    try
    {
    DocX wordDoc = DocX.Create(filePath, DocumentTypes.Document);
    return wordDoc;
    }
    catch
    {
    return null;
    }
    }
    //保存新文件
    public static void SaveDocument(DocX wordDoc)
    {
    try
    {
    wordDoc.Save();
    }
    catch
    {
    }
    }


    //插入表格,bookmark书签
    public static Table InsertTable(DocX wordDoc, string bookmark, int rows, int columns, float width)
    {
    try
    {
    return ReplaceWithTable(wordDoc, bookmark, rows, columns);

    }
    catch
    {
    return null;
    }
    }

    //给表格插入rows行,n为表格的序号
    public static void AddRow(DocX wordDoc, int n, int rows)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }
    for (int i = 0; i < rows; i++)
    {
    try
    {
    wordDoc.Tables[n].InsertRow(wordDoc.Tables[n].Rows[1]);
    }
    catch
    {
    wordDoc.Tables[n].InsertRow();
    }
    }
    }
    catch
    {
    }
    }
    //给表格插入rows行,n为表格的序号
    public static void AddRow(DocX wordDoc, int n, int start, int rows)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }
    for (int i = 0; i < rows; i++)
    {
    wordDoc.Tables[n].InsertRow(wordDoc.Tables[n].Rows[start-1], start);
    }
    }
    catch
    {
    }
    }

    //给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
    public static void InsertCell(DocX wordDoc, int n, int row, int column, string value, string bgcolor, string color)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }

    wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].RemoveParagraphAt(0);
    Paragraph p = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].InsertParagraph(value);
    }
    catch
    {
    }
    }

    //给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
    public static void InsertCell(DocX wordDoc, int n, int row, int column, string value)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }

    wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].RemoveParagraphAt(0);
    Paragraph p = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].InsertParagraph(value);
    }
    catch
    {
    }
    }

    /// <summary>
    /// 表格内居中Alignmentcenter
    /// </summary>
    /// <param name="wordDoc"></param>
    /// <param name="n"></param>
    /// <param name="row"></param>
    /// <param name="column"></param>
    /// <param name="value"></param>
    public static void InsertCellAC(DocX wordDoc, int n, int row, int column, string value)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }

    Cell c = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1];

    c.RemoveParagraphAt(0);
    c.VerticalAlignment = VerticalAlignment.Center;
    c.InsertParagraph(value);
    c.Paragraphs[0].Alignment = Alignment.center;

    }
    catch
    {
    }
    }

    //给表格插入rows行,n为表格的序号
    public static void RemoveRow(DocX wordDoc, int n, int rows)
    {
    try
    {
    n = n - 1;
    if (n < 0)
    {
    return;
    }
    if (n >= wordDoc.Tables.Count)
    {
    return;
    }

    wordDoc.Tables[n].RemoveRow(rows);
    }
    catch
    {
    }
    }
    //插入图片
    public static void InsertPicture(DocX wordDoc, string bookmark, string picturePath, int width, int hight)
    {
    try
    {
    ReplaceWithImage(wordDoc, bookmark, picturePath, width, hight);
    }
    catch
    {
    }

    }

    //插入一段文字,text为文字内容
    public static void InsertText(DocX wordDoc, string bookmark, string text)
    {
    try
    {
    wordDoc.ReplaceText(bookmark, text);
    }
    catch
    {

    }
    }

    //插入一段文字,text为文字内容
    public static void InsertWordFile(DocX wordDoc, string bookmark, string WordFileName, string outFilePath)
    {
    try
    {
    wordDoc.InsertParagraph().InsertPageBreakAfterSelf();
    wordDoc.InsertDocument( DocX.Load( WordFileName));
    }
    catch
    {

    }
    }

    //插入一段文字,text为文字内容
    public static void InsertWordFile(DocX wordDoc, DocX InsertDoc)
    {
    try
    {
    wordDoc.InsertDocument(InsertDoc);
    // wordDoc.InsertParagraph().InsertPageBreakAfterSelf();
    }
    catch
    {

    }
    }
    /// <summary>
    /// 提供下载
    /// </summary>
    /// <param name="path"></param>
    /// <param name="page"></param>
    /// <param name="isDelete"></param>
    public static void UploadWord(string path, System.Web.UI.Page page, bool isDelete)
    {
    try
    {
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    page.Response.Clear();
    page.Response.Charset = "GB2312";
    page.Response.ContentEncoding = System.Text.Encoding.UTF8;
    // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
    page.Response.AddHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(file.Name));
    // 添加头信息,指定文件大小,让浏览器能够显示下载进度
    page.Response.AddHeader("Content-Length", file.Length.ToString());
    // 指定返回的是一个不能被客户端读取的流,必须被下载
    page.Response.ContentType = "application/ms-excel";
    // 把文件流发送到客户端
    page.Response.WriteFile(file.FullName);
    page.Response.Flush();

    if (isDelete)
    {
    System.IO.File.Delete(path);
    }
    // 停止页面的执行
    page.Response.End();
    }
    catch
    {
    }
    }

  • 相关阅读:
    全面解释java中StringBuilder、StringBuffer、String类之间的关系
    如何解决Java.lang.NoClassDefFoundError--第一部分
    Java中Vector和ArrayList的区别
    深入研究java.lang.ThreadLocal类
    Frame.pack()与frame.validate()方法的区别
    Oracle中start with...connect by子句的用法
    Java 的swing.GroupLayout布局管理器的使用方法和实例
    Java SE 6.0实现高质量桌面集成开发
    苹果App Store开发者帐户从申请,验证,到发布应用(2)
    苹果App Store开发者帐户从申请,验证,到发布应用(1)
  • 原文地址:https://www.cnblogs.com/sanshengshitouhua/p/14354931.html
Copyright © 2011-2022 走看看