我创建了一个电子表格(MSDN 的示例),现在我想将数据从数据集填充到此工作表中。填充数据后,我想根据可用数据绘制图表。我不想使用数据透视表,我只想绘制来自数据集的任何数据的图表。我以前从未使用过电子表格,也找不到合适的示例来获得帮助。我这里有两个问题
- 如何从数据集中填充电子表格中的数据
- 如何根据可用数据绘制图表(任何图表示例)
任何人的帮助都会得到真正的赞赏。
以下是我的代码片段
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();
}