2

我创建了一个电子表格(MSDN 的示例),现在我想将数据从数据集填充到此工作表中。填充数据后,我想根据可用数据绘制图表。我不想使用数据透视表,我只想绘制来自数据集的任何数据的图表。我以前从未使用过电子表格,也找不到合适的示例来获得帮助。我这里有两个问题

  1. 如何从数据集中填充电子表格中的数据
  2. 如何根据可用数据绘制图表(任何图表示例)

任何人的帮助都会得到真正的赞赏。

以下是我的代码片段

Public void CreateSpreadSheet()
{
    DataSet dataSet = GetDatasetForSpreadSheetChart();
    int noOfRows = dataSet.Tables["SpreadSheetTestTable"].Rows.Count;
    int noOfColumns = dataSet.Tables["SpreadSheetTestTable"].Columns.Count;

    // Create a spreadsheet document by supplying the filepath.
    SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filePath,SpreadsheetDocumentType.Workbook);

    // Add a WorkbookPart to the document.
    WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
    workbookpart.Workbook = new Workbook();

    // Add a WorksheetPart to the WorkbookPart.
    WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
    worksheetPart.Worksheet = new Worksheet(new SheetData());

    // Add Sheets to the Workbook.
    Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
    AppendChild<Sheets>(new Sheets());

    // Append a new worksheet and associate it with the workbook.
    Sheet sheet = new Sheet()
    {
        Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),SheetId = 1,Name = "mySheet"
    };

    sheets.Append(sheet);

    // Get data from dataset and insert it in spreadsheet
    SheetData sheetData = sheet.GetFirstChild<SheetData>();
    for (int c = 0; c < noOfColumns; c++)
    {
       string headerName = dataSet.Tables["SpreadSheetTestTable"].Columns[c].ToString();
    }

    workbookpart.Workbook.Save();

    // Close the document.
    spreadsheetDocument.Close();
 }
4

2 回答 2

1
public void CreateExcelDocFromDatatable(DataTable dataTable)
{
    object misValue = System.Reflection.Missing.Value;
    Excel.Application xlApp = new Excel.ApplicationClass();
    Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
    Excel.Worksheet xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    int iCol = 0;
    foreach (DataColumn c in dataTable.Columns)
    {
        iCol++;
        xlWorkSheet1.Cells[1, iCol] = c.ColumnName;
    }

    int iRow = 0;
    foreach (DataRow r in dataTable.Rows)
    {
        iRow++;
        iCol = 0;
        foreach (sd.DataColumn c in mdtOutput.Columns)
        {
            string cellData = r[c.ColumnName].ToString();
            iCol++;
            xlWorkSheet1.Cells[iRow + 1, iCol] = r[c.ColumnName];
        }
    }

    xlWorkSheet1.Activate();
    var range = xlWorkSheet1.get_Range("2:2",misValue);
    range.Select();
    xlApp.ActiveWindow.FreezePanes = true;
    range = xlWorkSheet1.get_Range("1:1", misValue);
    range.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);

    mXlWorkBook.SaveAs(outputFilePath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
    xlApp.Visible = true;
    xlWorkSheet1.Activate();
}




private void CreateExcelCharts()
{
    object misValue = System.Reflection.Missing.Value;
    Excel.Workbook xlWorkBook = xlApp.Workbooks.Add(misValue);
    Excel.Worksheet chartsSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
    chartsSheet.DisplayRightToLeft = false;
    chartsSheet.Name = "Charts";

    Excel.ChartObjects chartObjs = (Excel.ChartObjects)chartsSheet.ChartObjects(Type.Missing);
    Excel.ChartObject chartObj = chartObjs.Add(200, 40, 300, 300);
    Excel.Chart xlChart = chartObj.Chart;
    Excel.Range range = chartsSheet.get_Range("B2", "C7");
    xlChart.SetSourceData(range, misValue);
    xlChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xl3DPie;
    mXlWorkBook.Save();
}

然后您可以使用图表属性,您可以在其中几乎控制 Excel UI 中的所有内容(图表标题、轴、网格线、颜色等)

于 2012-07-05T10:37:01.357 回答
0

对于这种情况,我找到了一个非常好的解决方案。Excel中的表格可以从以下链接 http://www.nitrix-reloaded.com/2010/09/26/creating-excel-files-from-dataset-using-openxml-20-c-sharp/

而不是通过使用http://spreadsheetlight.com/库可以在 Excel 中绘制任何类型的图表/图形。

可以根据场景进行更改。例如为图表创建提供开始和结束单元格值等。在我的场景中,我使用过运行时值,但我不知道我的 Excel 表中有哪些列和多少列。如果有人需要,我可以提供我的场景的代码片段。

问候!

于 2012-07-18T08:56:18.910 回答