2011-11-05 2 views
0

don net app에서 보고서 파일을 생성해야합니다. 테이블 형식으로 데이터를 넣어야합니다. 대개 Microsoft 보고서 뷰어 컨트롤을 사용하길 기대합니다. 하지만 sqlite를 사용하고 있으며 자동으로 보고서를 생성 할 수 없습니다. 또한 내 보고서는 특정 테이블이 아닌 여러 테이블을 기반으로합니다. 그래서 RichTextFormat 또는 Word .doc을 사용하기로 결정했습니다.net에서 rtf 또는 word를 사용하여 서식이 지정된 (테이블) 보고서 생성

은 내가 행이 고정 된 것이 아니라 더 작은 수 있습니다 물론이

------------------------------------------------------------------------- 
| Column 1  | Column 2  | Column 3  | Column 4  | 
------------------------------------------------------------------------- 
|     |     |     |     | 
|     |     |     |     | 
|     |     |     |     | 
------------------------------------------------------------------------- 

과 같은 보고서를 작성해야합니다. 이 일을하는 가장 좋은 방법을 안내해 주시겠습니까?

감사

+0

답을 얻으려면 yes라고 표시하십시오. –

답변

0

interop 라이브러리를 사용하고 Word 문서에도 직접 쓸 수 있습니다. 특히 문서 구조가 아주 간단하다면. 단점 : 보고서를 생성 할 컴퓨터에 Word를 설치해야합니다. 아래 예제에서는 어셈블리를 추가해야합니다. Microsoft.Office.Interop.Word (... \ Office12 \ Microsoft.Office.Interop.Word.dll), Office (... \ Office12 \ Office.dll).

using System; 
using System.Collections.Generic; 
using System.Text; 
using Microsoft.Office.Interop.Word; 
using Microsoft.Office.Core; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Diagnostics; 

namespace WriteToWordSO 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      _Application app = null; 
      Documents docs = null; 
      Document doc = null; 
      Range range = null; 
      Tables tables = null; 
      Table table = null; 

      object oMissing = System.Reflection.Missing.Value; 
      object oFalse = false; 
      object oTrue = true; 
      object fileName = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "tmp.doc"); 
      object fileFormat = WdSaveFormat.wdFormatDocument; 

      FileInfo fi = new FileInfo(fileName.ToString()); 
      if (fi.Exists) fi.Delete(); 

      int rows = 4, cols = 5; 

      try 
      { 
       app = new ApplicationClass(); 
       app.Visible = false; 
       app.DisplayAlerts = WdAlertLevel.wdAlertsNone; 
       app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable; 

       docs = app.Documents; 
       doc = docs.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

       range = doc.Range(ref oMissing, ref oMissing); 

       tables = doc.Tables; 

       object oAutoFitCell = WdDefaultTableBehavior.wdWord9TableBehavior; 
       object oAutoFitWindow = WdAutoFitBehavior.wdAutoFitWindow; 

       table = tables.Add(range, rows, cols, ref oAutoFitCell, ref oAutoFitWindow); 

       Cell c = null; 
       Range cellRange = null; 

       // header row 
       for (int i = 1; i < cols + 1; i++) 
       { 
        c = table.Cell(1, i); 
        cellRange = c.Range; 
        cellRange.Text = "Header " + i.ToString(); 
        cellRange.Bold = 1; 
        Marshal.ReleaseComObject(c); 
        Marshal.ReleaseComObject(cellRange); 
       } 

       // data rows 
       for (int i = 1; i < cols + 1; i++) 
       { 
        for (int j = 1; j < rows; j++) 
        { 
         c = table.Cell(j + 1, i); 
         cellRange = c.Range; 
         cellRange.Text = "Cell " + i.ToString() + j.ToString(); 
         Marshal.ReleaseComObject(c); 
         Marshal.ReleaseComObject(cellRange); 
        } 
       } 

       doc.SaveAs(ref fileName, ref fileFormat, 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); 

       ((_Document)doc).Close(ref oFalse, ref oMissing, ref oMissing); 

       ((_Application)app).Quit(ref oFalse, ref oMissing, ref oMissing); 

       Process.Start(fileName.ToString()); 
      } 
      finally 
      { 
       // frees memory 

       GC.Collect(); 
       GC.WaitForPendingFinalizers(); 
       GC.Collect(); 
       GC.WaitForPendingFinalizers(); 

       // its very important to release all used object, 
       // otherwise some complications may appear 
       if (table != null) Marshal.ReleaseComObject(table); 
       table = null; 

       if (tables != null) Marshal.ReleaseComObject(tables); 
       tables = null; 

       if (range == null) Marshal.ReleaseComObject(range); 
       range = null; 

       if (doc != null) Marshal.ReleaseComObject(doc); 
       doc = null; 

       if (docs != null) Marshal.ReleaseComObject(docs); 
       docs = null; 

       if (app != null) Marshal.ReleaseComObject(app); 
       app = null; 
      } 
     } 
    } 
} 
0

당신은 "오피스 오픈 XML"를 사용하고 자신의 DOCX 라이터를 구축 할 수 있습니다 자세한 내용은이 위키 url를 보라. dotnet에서이 작업에 대한 특정 네임 스페이스를 가져옵니다. 희망이 당신을 도울 것입니다.