将数据库表数据绑定到电子表格的倍数表 [英] Bind Database tables Data to Multiples sheets of Spreadsheet
本文介绍了将数据库表数据绑定到电子表格的倍数表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张3张电子表格,名为T1,T2和T3。
我在数据集中有三个数据表,名为T1,T2和T3。
我的要求是,将T1数据表数据插入T1工作表。:将T2数据表数据插入T2表:将T3数据表数据插入T3表。
这里我使用Open XMl SDK创建电子表格。
如果有人知道请帮助我们......我很震惊......
Hi,
I have a spreadsheet with 3 sheets named as T1,T2 and T3.
I have three datatables in dataset named as T1,T2 and T3.
My requirement is, Insert T1 Datatable data into T1 Sheet.:Insert T2 Datatable data into T2 Sheet : Insert T3 Datatable data into T3 Sheet.
Here i am creted spreadsheet using Open XMl SDK.
If anyone knows please help me guys.....I'm struck with that work......
推荐答案
检查下面的代码,它将所有DataTable转换为工作簿中的Excel工作表。我已经测试并且工作正常。信用转到此用户 [ ^ ]
check below code, it is convert all the DataTables to Excel sheets in workbook. I have tested and working fine. credit goes to This User[^]
private void ExportDataSet(DataSet ds, string destination)
{
using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
foreach (System.Data.DataTable table in ds.Tables)
{
var sheetPart = workbook.WorkbookPart.AddNewPart<worksheetpart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<documentformat.openxml.spreadsheet.sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if (sheets.Elements<documentformat.openxml.spreadsheet.sheet>().Count() > 0)
{
sheetId =
sheets.Elements<documentformat.openxml.spreadsheet.sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<string> columns = new List<string>();
foreach (System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString());
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
}
}</string></string></documentformat.openxml.spreadsheet.sheet></documentformat.openxml.spreadsheet.sheet></documentformat.openxml.spreadsheet.sheets></worksheetpart>
这篇关于将数据库表数据绑定到电子表格的倍数表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文