2012-09-25 82 views
0

我想通過Silverlight 4.0中的給定字符串設置WorkSheet來將元素(也許是gridview,List等)導出爲.xls文件。 我該如何解決這個問題?如何按名稱設置目標工作表?

以下是通過使用RadGridView API導出功能...

public RadGridView Grid { get; private set; } 
void ExportWithoutDetail() 
{ 
    SaveFileDialog saveDialog = new SaveFileDialog(); 
    saveDialog.Filter = ReadFilter(); 
    saveDialog.FilterIndex = 4; 


    if (saveDialog.ShowDialog() == true) 
    { 
     SetAllColumnVisibile(); 
     Stream stream = null; 
     try 
     { 
      using (stream = saveDialog.OpenFile()) 
      { 
       GridViewExportOptions opt = CreateExportOptions(); 

       switch (saveDialog.FilterIndex) 
       { 
        case 1: //*.txt 
         opt.Format = ExportFormat.Text; 
         break; 
        case 2: //*.html 
         opt.Format = ExportFormat.Html; 
         break; 
        case 3: //*.csv 
         opt.Format = ExportFormat.Csv; 
         break; 
        case 4: //*.xls 
         opt.Format = ExportFormat.ExcelML; 
         break; 
        case 5: //*.xml 
         opt.Format = ExportFormat.ExcelML; 
         break; 
       } 
       Grid.Export(stream, opt); 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      if (stream != null) 
       stream.Close(); 

     } 
    } 
} 

回答

1

您可以通過使用此代碼的網格或列表導出到Excel,

if ((xamGridtemplateDet.ItemsSource).Count > 0) 
     { 
      Workbook wrkbook = new Workbook(); 

      Worksheet wrkSHT = wrkbook.Worksheets.Add("Sheet 1"); 

      wrkSHT.DisplayOptions.PanesAreFrozen = true; 
      wrkSHT.DisplayOptions.FrozenPaneSettings.FrozenRows = 1; 

      wrkSHT.DefaultColumnWidth = 5000; 
      int currentcolumn = 0; 
      foreach (TemplateDetailUI column in xamGridtemplateDet.ItemsSource) 
      { 
       if (column != null) 
       { 
        SetCellValue(wrkSHT.Rows[0].Cells[currentcolumn], column.ColumnName); 
        currentcolumn++; 
       } 
      } 


      SaveExport(wrkbook); 

}

 public void SaveExport(Workbook dataworkbook) 
    { 
     try 
     { 
      bool? showDialog = this.dialog.ShowDialog(); 
      if (showDialog == true) 
      { 
       using (System.IO.Stream exportstream = dialog.OpenFile()) 
       { 
        dataworkbook.Save(exportstream); 
        exportstream.Close(); 
       } 


      } 
     } 
     catch (Exception ex) 
     { 

      throw ex; 
     } 


    } 
+0

謝謝你的回答!我通過Silverlight 4編寫了這段代碼。工作簿好像沒有在這個版本中提供......嗯,我已經放棄嘗試使用Api,但是將文本保存爲.xls。 – user1531714