Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

happysoft2018/sql2excel

Repository files navigation

SQL2Excel - Generate Excel Files from SQL Query Results

A Node.js-based tool for generating Excel files from SQL query results.

🎯 Key Features

  • 📊 Multi-Sheet Support: Save multiple SQL query results in separate sheets within one Excel file
  • 🎨 Excel Styling: Detailed styling for header/data areas including fonts, colors, borders, alignment
  • 🔗 Multiple DB Connections: Use different database connections for each sheet
  • 📝 Variable System: Use variables in queries for dynamic query generation
  • 🔄 Dynamic Variables: Extract values from database in real-time for dynamic query generation
  • 🔄 Query Reuse: Define common queries and reuse them across multiple sheets
  • ⚙️ Parameter Override: Override query definition parameters for each sheet
  • 📋 Auto Table of Contents: Automatically generate table of contents sheet with hyperlinks
  • 📊 Aggregation Features: Automatic aggregation and display of counts by specified column values
  • 🚦 Query Limits: Row count limiting for large data processing
  • 🖥️ CLI Interface: Simple command-line tool execution
  • 🪟 Windows Batch Files: Batch files for Windows users
  • 📄 XML/JSON Support: Flexible configuration file format support

🚀 Quick Start

1. Installation

npm install

2. Database Configuration

Configure database connection information in config/dbinfo.json:

{
 "dbs": {
 "sampleDB": {
 "server": "localhost",
 "port": 1433,
 "database": "SampleDB",
 "user": "sa",
 "password": "yourpassword",
 "options": {
 "encrypt": false,
 "trustServerCertificate": true
 }
 }
 }
}

3. Generate Excel File

# Execute via CLI command
node src/excel-cli.js export --xml ./queries/queries-sample.xml
# Or execute via NPM script
npm run export -- --xml ./queries/queries-sample.xml
# Or execute via Windows batch file
실행하기.bat

4. Main CLI Commands

# Generate Excel file
node src/excel-cli.js export --xml ./queries/sample.xml
# Validate query file
node src/excel-cli.js validate --xml ./queries/sample.xml
# Test database connection
node src/excel-cli.js list-dbs
# Help
node src/excel-cli.js help

📚 Documentation

For detailed usage and advanced features, refer to the following documents:

💡 Usage Examples

XML Configuration File Example (with Dynamic Variables)

<queries>
 <excel db="sampleDB" output="output/SalesReport.xlsx">
 <header>
 <font name="Arial" size="12" color="FFFFFF" bold="true"/>
 <fill color="4F81BD"/>
 </header>
 </excel>
 
 <!-- Regular variables -->
 <vars>
 <var name="startDate">2024年01月01日</var>
 <var name="endDate">2024年12月31日</var>
 </vars>
 
 <!-- Dynamic variables -->
 <dynamicVars>
 <dynamicVar name="activeCustomers" description="Active customer list">
 <![CDATA[
 SELECT CustomerID, CustomerName, Region
 FROM Customers WHERE IsActive = 1
 ]]>
 </dynamicVar>
 </dynamicVars>
 
 <sheet name="MonthlySales" use="true" aggregateColumn="Month">
 <![CDATA[
 SELECT MONTH(OrderDate) as Month, SUM(TotalAmount) as Sales
 FROM Orders 
 WHERE YEAR(OrderDate) = 2024
 AND CustomerID IN (${activeCustomers.CustomerID})
 GROUP BY MONTH(OrderDate)
 ]]>
 </sheet>
</queries>

Variable Usage Example

node src/excel-cli.js export --xml ./queries/sales-report.xml \
 --var "startDate=2024年01月01日" \
 --var "endDate=2024年06月30日"

Dynamic Variables

The tool supports dynamic variables that can extract data at runtime and use it in queries:

Variable Types

Type Description Access Pattern Default
column_identified Extract all columns as arrays keyed by column name ${varName.columnName} ✅ Yes
key_value_pairs Extract first two columns as key-value pairs ${varName.key} No

Usage Examples

<!-- Using column_identified (default) -->
<dynamicVar name="customerData" description="Customer information">
 <![CDATA[
 SELECT CustomerID, CustomerName, Region FROM Customers
 ]]>
 <!-- type omitted - defaults to column_identified -->
</dynamicVar>
<!-- Using key_value_pairs -->
<dynamicVar name="statusMapping" description="Status mapping">
 <![CDATA[
 SELECT StatusCode, StatusName FROM StatusCodes
 ]]>
 <type>key_value_pairs</type>
</dynamicVar>
-- In your sheet queries
SELECT * FROM Orders 
WHERE CustomerID IN (${customerData.CustomerID})
 AND Status IN (${statusMapping.StatusCode})

🔧 Environment Requirements

  • Node.js 16.0 or higher
  • SQL Server 2012 or higher
  • Appropriate database permissions

📞 Support

License

MIT License

Copyright (c) 2024 MSSQL Data Migration Tool

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Version: v1.2.2 | Last Updated: 2025年08月20日

About

A Node.js-based tool for generating Excel files from SQL query results.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 71.7%
  • Batchfile 17.8%
  • TSQL 10.5%

AltStyle によって変換されたページ (->オリジナル) /