如何使用C#将水晶报表中的可编辑表导出到word文档?

时间:2012-08-24 07:08:56

标签: c# sql crystal-reports-2008 .doc

我尝试导出的表是从Visual Studio 2008中的SQL数据库导入到Crystal Reports中的,我使用C#创建Web应用程序。我试图将它导出到word文档,但保持表格式,我可以稍后在MS WORD编辑。但是,我得到的只是一堆文本框。

我尝试将其导出为ex​​cel文档然后复制到word,这样就可以了,但我需要能够通过C#代码完成这项工作。

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

我非常感谢任何帮助!我谷歌搜索了几天,我仍然没有找到合适的解决方案......

1 个答案:

答案 0 :(得分:0)

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

private void 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);