2014-08-30 2 views
0

'export als xls'에 다른 레이아웃을 사용할 수 있는지 궁금합니다. 그 이유는 멋지게 보이는 것 (예 : 눈에 보이는 raport, pdf 및 word)이 때로는 Excel에서 실용적인 것과 매우 다르다는 것입니다. 설명서를 살펴 보았으므로 아무 소용이 없습니다. 어떤 아이디어?C# ReportViewer - xls에 대한 다른 레이아웃

답변

0

수출에 동의합니다. Excel이나 Word는 명시 적 배치가있는 "개체"이외에는 아무 작업도 수행하지 않으므로 실용적인 것은 없습니다. 실제로 보고서 컨텍스트에서 데이터를 가져 와서 원하는 형식으로 직접 덤프하기 위해 내보내기 동작에 대한 자체 훅을 작성했습니다.

이제 메모 ... 내 주요 보고서 관리자의 코드 스 니펫이 있습니다. 모든 보고서가 파생되므로 같은 보고서 미리보기 프레임 워크를 반복해서 작성할 필요가 없습니다. 내 수업에서는 가상 메서드 GenerateData()가 있고 각 서브 클래 싱 된 보고서는 데이터베이스를 쿼리하고 데이터 테이블을 준비한 다음 주 보고서의 "DataSet"에 데이터 테이블을 추가하고 보고서를 계속 보냅니다.

내보내기 옵션은 각 하위 클래스 보고서가 자신의 데이터 테이블이 무엇인지 알고 있고 내보낼 열에 몇 가지 제한 사항이있는 가상 함수이며 최종 내보내기를 위해 TableToCSV() 메서드를 호출합니다.

희망 사항을 통해 원하는 아이디어를 얻을 수 있습니다.

// hook for when exporting is done. 
reportViewer.ReportExport += reportViewer_ReportExport; 


void reportViewer_ReportExport(object sender, ReportExportEventArgs e) 
{ 
    // Prepare an Export path for the user's desktop 
    exportPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
      + "\\SomeExportPath\\"; 

    // In case multiple exports, time-stamp the file name too 
    timeMark = DateTime.Now.ToString("yyyyMMddTHHmmss"); 

    if (sender != null) 
    { 
     switch(e.Extension.Name) 
     { 
      case "Excel": 
       ExportForExcel(); 
       e.Cancel = true; 
       Messagebox("Exported Excel(CSV) to your desktop in the \r\n" 
         + "folder 'SomeExportPath\\SomeFile.csv'"); 
       break; 
     } 
    } 
} 

protected virtual void ExportForExcel() 
{} 

protected void TableToCSV(DataTable dt, string FinalFileName) 
{ 
    StringBuilder sb = new StringBuilder(); 
    IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName); 
    sb.AppendLine(string.Join(",", columnNames)); 

    foreach (DataRow row in dt.Rows) 
    { 
     IEnumerable<string> fields = row.ItemArray.Select(field => 
      string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\"")); 
     sb.AppendLine(string.Join(",", fields)); 
    } 
    File.WriteAllText(exportPath + FinalFileName + ".csv", sb.ToString()); 

}