2012-08-24 41 views
1

我試圖導出的表格從Visual Studio 2008中的SQL數據庫導入到Crystal Reports中,我使用C#製作Web應用程序。我試圖將它導出爲word文檔,但保留表格格式,我稍後可以在MS WORD中進行編輯。但是,我得到的只是一堆文本框。如何使用C#將水晶報表中的可編輯表格導出爲Word文檔?

我試圖將其導出爲Excel文檔,然後複製到Word中,並且工作正常,但我需要能夠僅通過C#代碼完成此操作。

所以問題是:有沒有更好的方式來直接從水晶報表導出的Word文檔中有一個可編輯的表格,或者有解決方案如何複製excel表格,但只能通過c#代碼來完成?

我真的很感謝任何幫助!我GOOGLE了幾天,我仍然沒有找到一個適當的解決方案...

回答

0

我設法插入Excel文檔到Word,然後保存它。只有THA數據路徑上的小問題,需要一點點的調整,但除此之外,它的工作原理:)

私人無效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);