0

我想将一个元素(可能是 gridview、List 等)导出为 .xls 文件,并通过 Silverlight 4.0 中的给定字符串设置 WorkSheet。我怎么解决这个问题?

以下是使用 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();

        }
    }
}
4

1 回答 1

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;
        }


    }
于 2012-10-17T10:46:04.027 回答