1

我试图导出的表是从 Visual Studio 2008 中的 SQL 数据库导入 Crystal Reports 中的,我在其中使用 C# 制作 Web 应用程序。我正在尝试将其导出到 Word 文档,但要保留表格格式,以便稍后在 MS WORD 中进行编辑。但是,我得到的只是一堆文本框。

我尝试将其导出为 excel 文档,然后复制到 word 中,这很有效,但我需要能够仅通过 C# 代码来做到这一点。

所以问题是:有没有更好的方法可以直接在从水晶报表导出的word文档中编辑表格,或者是否有解决方案如何在word中复制excel表格但只能通过c#代码这样做?

我真的很感激任何帮助!我在谷歌上搜索了几天,但我仍然没有找到合适的解决方案......

4

1 回答 1

0

我设法将 Excel 文档插入 Word,然后保存。仅数据路径的小问题需要稍微调整,但除此之外它还有效:)

私人无效 ConvertExcelToWordAndAutoSave() {

        Excel.Application xlApp;
        Excel.Workbook xlWorkBook;
        Excel.Worksheet xlWorkSheet;
       //TT Excel.Range range;



        object misValue = System.Reflection.Missing.Value;

        DateTime dt = new DateTime();
        dt = DateTime.Now;

            // open excel 
            xlApp = new Excel.ApplicationClass();
            //Change THE LOCATION!
            xlWorkBook = xlApp.Workbooks.Open("C:\\Documents and Settings\\Student\\Desktop\\ExportFiles\\Excel.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet=(Excel.Worksheet)xlWorkBook.ActiveSheet;




            // open word 
            object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */

            //Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;

            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);



            Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            oPara1.Range.Text = "Snimeno na:" + " " + dt+"\n";
            oPara1.Range.Font.Bold = 1;
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();




            xlWorkSheet.get_Range("A1", "N49").Copy(Missing.Value);

            oWord.Selection.Paste();

            oWord.Selection.TypeParagraph();
           //The textBox is for the name of the new Word document
            if (TextBox1.Text == "")
                TextBox1.Text = "Document1";
         object fileName = @"C:\\Documents and Settings\\Student\\Desktop\\ExportFiles\\"+TextBox1.Text+".docx"; 

            oDoc.SaveAs(ref fileName,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
于 2012-08-27T06:55:06.467 回答