使用与数据库连接的OpenXml创建Excel文件

时间:2019-03-07 11:38:26

标签: c# sql-server excel database openxml

我想用openXML库创建一个Excel文件。文档应与数据库连接,并在单张/多张中包含由不同查询接收的不同数据。结果示例:

SELECT * FROM dbo.MyTable1 -- data from first table
SELECT * FROM dbo.MyTable2 -- data from second table

文档: enter image description here

到目前为止,我有一些代码的工作部分:

Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

            ConnectionsPart connPart = workbookpart.AddNewPart<ConnectionsPart>();
            connPart.Connections = new Connections();

            var connection = new Connection()
            {
                Id = 1,
                Name = "Connection",
                Type = 5, //ODBC
                SaveData = true,
                RefreshOnLoad = true,
                RefreshedVersion = 5,
                MinRefreshableVersion = 1,
                Background = true,
                DatabaseProperties = new DatabaseProperties
                {
                    Connection =
                        "my-connection-string",
                    Command = "SELECT * FROM dbo.MyTable1",
                },
            };

            connPart.Connections.Append(connection);

            QueryTablePart qt = worksheetPart.AddNewPart<QueryTablePart>();

            qt.QueryTable = new QueryTable()
            {
                Name = "Connection",
                ConnectionId = connection.Id,
                AutoFormatId = 16,
                ApplyNumberFormats = true,
                ApplyBorderFormats = true,
                ApplyFontFormats = true,
                ApplyPatternFormats = true,
                ApplyAlignmentFormats = false,
                ApplyWidthHeightFormats = false,
                AdjustColumnWidth = true,
                Headers = false,
                RefreshOnLoad = true
            };

            // 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);
            sheets.Append(qt.QueryTable);

            DefinedNames definedNames = new DefinedNames();
            // Create a new range (name matching the QueryTable name) 
            DefinedName definedName = new DefinedName() { Name = "Connection", Text = "mysheet!$B$2:$B$2",  };

            definedNames.Append(definedName);
            workbookpart.Workbook.Append(definedNames);


            workbookpart.Workbook.Save();

对于使用单个命令的单个连接正常。问题是我不知道如何更改代码以使用多个命令(查询)。当我尝试添加第二个连接和第二个QueryTablePart时,创建了文档,但是内容被破坏,错误提示QueryTablePart应该有一个根元素。有人能帮我吗?谢谢

1 个答案:

答案 0 :(得分:0)

好吧,总是,我自己找到了解决方案。为了正常工作,您必须在构造函数中传递QueryTablePart Id。工作代码部分:

var connection = new Connection()
            {
                Id = 1,
                Name = "Connection",
                Type = 5, //ODBC
                SaveData = true,
                RefreshOnLoad = true,
                RefreshedVersion = 5,
                MinRefreshableVersion = 1,
                Background = true,
                DatabaseProperties = new DatabaseProperties
                {
                    Connection =
                        "connection-string",
                    Command = "SELECT * FROM dbo.MyTable1",
                },
            };

            var connection1 = new Connection()
            {
                Id = 2,
                Name = "Connection1",
                Type = 5, //ODBC
                SaveData = true,
                RefreshOnLoad = true,
                RefreshedVersion = 5,
                MinRefreshableVersion = 1,
                Background = true,
                DatabaseProperties = new DatabaseProperties
                {
                    Connection =
                        "connection-string",
                    Command = "SELECT * FROM dbo.MyTable2",
                },
            };
            connPart.Connections.Append(connection);
            connPart.Connections.Append(connection1);

            QueryTablePart qt = worksheetPart.AddNewPart<QueryTablePart>("part1");//IMPORTANT
            QueryTablePart qt2 = worksheetPart.AddNewPart<QueryTablePart>("part2");//IMPORTANT

            qt.QueryTable = new QueryTable()
            {
                Name = "Connection",
                ConnectionId = connection.Id,
                AutoFormatId = 16,
                ApplyNumberFormats = true,
                ApplyBorderFormats = true,
                ApplyFontFormats = true,
                ApplyPatternFormats = true,
                ApplyAlignmentFormats = false,
                ApplyWidthHeightFormats = false,
                AdjustColumnWidth = true,
                Headers = false,
                RefreshOnLoad = true
            };
            qt2.QueryTable = new QueryTable()
            {
                Name = "Connection1",
                ConnectionId = connection1.Id,
                AutoFormatId = 16,
                ApplyNumberFormats = true,
                ApplyBorderFormats = true,
                ApplyFontFormats = true,
                ApplyPatternFormats = true,
                ApplyAlignmentFormats = false,
                ApplyWidthHeightFormats = false,
                AdjustColumnWidth = true,
                Headers = false,
                RefreshOnLoad = true
            };

            // 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);
            sheets.Append(qt.QueryTable);
            sheets.Append(qt2.QueryTable);

            DefinedNames definedNames = new DefinedNames();
            // Create a new range (name matching the QueryTable name) 
            DefinedName definedName = new DefinedName() { Name = "Connection", Text = "mysheet!$B$2:$B$2",  };
            DefinedName definedName1 = new DefinedName() { Name = "Connection1", Text = "mysheet!$C$2:$C$2", };

            definedNames.Append(definedName);
            definedNames.Append(definedName1);
            workbookpart.Workbook.Append(definedNames);


            workbookpart.Workbook.Save();