Linq to xml导出Excel

Imports <xmlns="urn:schemas-microsoft-com:office:spreadsheet">
Imports <xmlns:o="urn:schemas-microsoft-com:office:office">
Imports <xmlns:x="urn:schemas-microsoft-com:office:excel">
Imports <xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">

Module Module1

    Sub Main()

        Dim db As New NorthwindDataContext


        Dim customers = From customer In db.Customers _
                        Order By customer.CompanyName _
                        Select <Row>
                                   <Cell><Data ss:Type="String"><%= customer.CompanyName %></Data></Cell>
                                   <Cell><Data ss:Type="String"><%= customer.Phone %></Data></Cell>
                                   <Cell><Data ss:Type="String"><%= customer.Country %></Data></Cell>
                               </Row>

        Dim sheet = <?xml version="1.0"?>
                    <?mso-application progid="Excel.Sheet"?>
                    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
                        xmlns:o="urn:schemas-microsoft-com:office:office"
                        xmlns:x="urn:schemas-microsoft-com:office:excel"
                        xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
                        xmlns:html="http://www.w3.org/TR/REC-html40">
                        <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
                            <Author>MSADMIN</Author>
                            <LastAuthor>MSADMIN</LastAuthor>
                            <Created>2007-10-23T23:40:11Z</Created>
                            <Company>Microsoft</Company>
                            <Version>12.00</Version>
                        </DocumentProperties>
                        <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
                            <WindowHeight>6600</WindowHeight>
                            <WindowWidth>12255</WindowWidth>
                            <WindowTopX>0</WindowTopX>
                            <WindowTopY>60</WindowTopY>
                            <ProtectStructure>False</ProtectStructure>
                            <ProtectWindows>False</ProtectWindows>
                        </ExcelWorkbook>
                        <Styles>
                            <Style ss:ID="Default" ss:Name="Normal">
                                <Alignment ss:Vertical="Bottom"/>
                                <Borders/>
                                <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
                                <Interior/>
                                <NumberFormat/>
                                <Protection/>
                            </Style>
                            <Style ss:ID="s62">
                                <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"
                                    ss:Bold="1"/>
                            </Style>
                        </Styles>
                        <Worksheet ss:Name="Sheet1">
                            <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount=<%= customers.Count + 1 %> x:FullColumns="1"
                                x:FullRows="1" ss:DefaultRowHeight="15">
                                <Column ss:AutoFitWidth="0" ss:Width="200"/>
                                <Column ss:AutoFitWidth="1" ss:Width="200"/>
                                <Column ss:AutoFitWidth="1"/>
                                <Row ss:StyleID="s62">
                                    <Cell><Data ss:Type="String">Name </Data></Cell>
                                    <Cell><Data ss:Type="String">Phone</Data></Cell>
                                    <Cell><Data ss:Type="String">Country</Data></Cell>
                                </Row>
                                <%= customers %>
                            </Table>
                            <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                                <PageSetup>
                                    <Header x:Margin="0.3"/>
                                    <Footer x:Margin="0.3"/>
                                    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
                                </PageSetup>
                                <Print>
                                    <ValidPrinterInfo/>
                                    <HorizontalResolution>300</HorizontalResolution>
                                    <VerticalResolution>300</VerticalResolution>
                                </Print>
                                <Selected/>
                                <Panes>
                                    <Pane>
                                        <Number>3</Number>
                                        <ActiveCol>2</ActiveCol>
                                    </Pane>
                                </Panes>
                                <ProtectObjects>False</ProtectObjects>
                                <ProtectScenarios>False</ProtectScenarios>
                            </WorksheetOptions>
                        </Worksheet>
                        <Worksheet ss:Name="Sheet2">
                            <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
                                x:FullRows="1" ss:DefaultRowHeight="15">
                            </Table>
                            <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                                <PageSetup>
                                    <Header x:Margin="0.3"/>
                                    <Footer x:Margin="0.3"/>
                                    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
                                </PageSetup>
                                <ProtectObjects>False</ProtectObjects>
                                <ProtectScenarios>False</ProtectScenarios>
                            </WorksheetOptions>
                        </Worksheet>
                        <Worksheet ss:Name="Sheet3">
                            <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
                                x:FullRows="1" ss:DefaultRowHeight="15">
                            </Table>
                            <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
                                <PageSetup>
                                    <Header x:Margin="0.3"/>
                                    <Footer x:Margin="0.3"/>
                                    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
                                </PageSetup>
                                <ProtectObjects>False</ProtectObjects>
                                <ProtectScenarios>False</ProtectScenarios>
                            </WorksheetOptions>
                        </Worksheet>
                    </Workbook>


        sheet.Save("C:\Customers.xml")
        Process.Start("C:\customers.xml")


    End Sub

End Module

关于作者: 王昕(QQ:475660) 在广州工作生活30余年。十多年开发经验,在Java、即时通讯、NoSQL、BPM、大数据等领域较有经验。
目前维护的开源产品:https://gitee.com/475660
原文地址:https://www.cnblogs.com/starcrm/p/1362339.html