/// <summary>
    /// Translates a paragraph into the form we want to see: preferably the chapter/paragraph number, otherwise the
    /// title itself will do.
    /// </summary>
    /// <param name="paragraph">The paragraph to be translated</param>
    /// <returns></returns>
    private static string GetHeading(Word.Paragraph paragraph)
    {
        string heading = "";

        // Try to get the list number, otherwise just take the entire heading text
        heading = paragraph.Range.ListFormat.ListString;
        if (string.IsNullOrEmpty(heading))
        {
            heading = paragraph.Range.Text;
            heading = Regex.Replace(heading, "\\s+$", "");
        }
        return(heading);
    }
Esempio n. 2
0
        public string CopyToWord()
        {
            object filename = @"C:\" + System.DateTime.Now.ToFileTime() + ".doc";

            Word._Application application = new Word.ApplicationClass();
            Word._Document    document    = application.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            foreach (Excel.Worksheet sheet in wbb.Sheets)
            {
                if (sheet.Index == wbb.Sheets.Count)
                {
                    continue;
                }
                for (int i = 1; i < sheet.UsedRange.Rows.Count; i++)
                {
                    Word.Paragraph para = document.Paragraphs.Add(ref missing);
                    string         text = "";
                    for (int j = 1; j < sheet.UsedRange.Columns.Count; j++)
                    {
                        Excel.Range range = (Excel.Range)sheet.Cells[i, j];
                        if (range.Value2 != null)
                        {
                            text += range.Value2.ToString() + "\t";
                            if (range.Value2.ToString().StartsWith("#picture"))
                            {
                                string[]    value = System.Text.RegularExpressions.Regex.Split(range.Value2.ToString(), "_");
                                Excel.Shape shape = sheet.Shapes.Item(int.Parse(value[1])) as Excel.Shape;
                                shape.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap);
                                Word.Paragraph picture = document.Paragraphs.Add(ref missing);
                                picture.Range.Paste();
                                picture.Format.CharacterUnitFirstLineIndent = 2;
                                picture.Range.InsertParagraphAfter();
                                text = "";
                                i   += int.Parse(value[2]) - 1;
                                break;
                            }
                        }
                    }
                    para.Range.Text = text;
                    para.Format.CharacterUnitFirstLineIndent = 2;
                    para.Range.InsertParagraphAfter();
                }
            }
            document.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            document.Close(ref missing, ref missing, ref missing);
            application.Quit(ref missing, ref missing, ref missing);
            return(filename.ToString());
        }
 /// <summary>
 /// Identifies whether a paragraph is a heading, based on its styling.
 /// Note: the documents we're reviewing are always in a certain format, we can assume that headers
 /// have a style named "Heading..." or "Kop..."
 /// </summary>
 /// <param name="paragraph">The paragraph to be evaluated.</param>
 /// <returns></returns>
 private static bool IsHeading(Word.Paragraph paragraph)
 {
     Word.Style style = paragraph.get_Style() as Word.Style;
     return(style != null && style.NameLocal.StartsWith("Heading") || style.NameLocal.StartsWith("Kop"));
 }