Java 销售数据分析系统:实现年、月、周、日统计和 Excel 导出

本系统使用 Java 语言开发,基于 MySQL 数据库,实现销售数据的统计分析功能,旨在帮助公司更好地了解业务状况。系统包含以下主要功能:

数据展示:

  1. 销售记录展示,带分页功能、模糊查询(产品、客户、区域、门店、日期)、最近的数据展示在第一条。

统计功能:

  1. 年度销售统计功能按钮
  2. 月度销售统计功能按钮
  3. 周度销售统计功能按钮
  4. 日度销售统计功能按钮

导出功能:

  1. 统计结果导出为 Excel 文件

系统架构:

本系统采用 Java 语言开发,使用 MySQL 数据库存储销售数据。主要功能模块包括:

  1. 数据读取模块:负责从 MySQL 数据库中读取销售记录数据。
  2. 数据处理模块:负责对销售数据进行统计分析,包括年、月、周、日统计。
  3. 数据展示模块:负责将统计结果以表格形式展示给用户。
  4. Excel 导出模块:负责将统计结果导出为 Excel 文件。

代码示例:

import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class SalesDataAnalysis {
    private static final String DB_URL = 'jdbc:mysql://localhost/sales_db';
    private static final String DB_USER = 'username';
    private static final String DB_PASSWORD = 'password';

    public static void main(String[] args) {
        // 连接数据库
        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            // 查询销售记录
            List<SaleRecord> saleRecords = getSaleRecords(conn, '', '', '', '', '');

            // 分页展示销售记录
            List<SaleRecord> paginatedRecords = getPaginatedSaleRecords(saleRecords, 1, 10);

            // 输出展示结果
            for (SaleRecord record : paginatedRecords) {
                System.out.println(record);
            }

            // 年度销售统计
            SalesStatistics yearlyStatistics = getYearlyStatistics(saleRecords);
            System.out.println(yearlyStatistics);

            // 月度销售统计
            SalesStatistics monthlyStatistics = getMonthlyStatistics(saleRecords);
            System.out.println(monthlyStatistics);

            // 周度销售统计
            SalesStatistics weeklyStatistics = getWeeklyStatistics(saleRecords);
            System.out.println(weeklyStatistics);

            // 日度销售统计
            SalesStatistics dailyStatistics = getDailyStatistics(saleRecords);
            System.out.println(dailyStatistics);

            // 导出统计结果到Excel文件
            exportStatisticsToExcel(yearlyStatistics, 'yearlyStatistics.xlsx');
            exportStatisticsToExcel(monthlyStatistics, 'monthlyStatistics.xlsx');
            exportStatisticsToExcel(weeklyStatistics, 'weeklyStatistics.xlsx');
            exportStatisticsToExcel(dailyStatistics, 'dailyStatistics.xlsx');

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static List<SaleRecord> getSaleRecords(Connection conn, String product, String customer, String region,
            String store, String date) throws SQLException {
        List<SaleRecord> saleRecords = new ArrayList<>();

        String sql = 'SELECT * FROM sales WHERE product LIKE ? AND customer LIKE ? AND region LIKE ? AND store LIKE ? AND date LIKE ? ORDER BY date DESC';
        try (PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, '%' + product + '%');
            stmt.setString(2, '%' + customer + '%');
            stmt.setString(3, '%' + region + '%');
            stmt.setString(4, '%' + store + '%');
            stmt.setString(5, '%' + date + '%');

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    SaleRecord record = new SaleRecord();
                    record.setProduct(rs.getString('product'));
                    record.setCustomer(rs.getString('customer'));
                    record.setRegion(rs.getString('region'));
                    record.setStore(rs.getString('store'));
                    record.setDate(rs.getString('date'));
                    saleRecords.add(record);
                }
            }
        }

        return saleRecords;
    }

    public static List<SaleRecord> getPaginatedSaleRecords(List<SaleRecord> saleRecords, int page, int pageSize) {
        int startIndex = (page - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, saleRecords.size());
        return saleRecords.subList(startIndex, endIndex);
    }

    public static SalesStatistics getYearlyStatistics(List<SaleRecord> saleRecords) {
        // 实现年度销售统计逻辑
        return new SalesStatistics();
    }

    public static SalesStatistics getMonthlyStatistics(List<SaleRecord> saleRecords) {
        // 实现月度销售统计逻辑
        return new SalesStatistics();
    }

    public static SalesStatistics getWeeklyStatistics(List<SaleRecord> saleRecords) {
        // 实现周度销售统计逻辑
        return new SalesStatistics();
    }

    public static SalesStatistics getDailyStatistics(List<SaleRecord> saleRecords) {
        // 实现日度销售统计逻辑
        return new SalesStatistics();
    }

    public static void exportStatisticsToExcel(SalesStatistics statistics, String fileName) {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet('Statistics');

        int rowNumber = 0;
        Row headerRow = sheet.createRow(rowNumber++);
        Cell cell = headerRow.createCell(0);
        cell.setCellValue('Product');
        // 添加其他统计字段的表头

        // 填充统计数据
        for (int i = 0; i < statistics.getProducts().size(); i++) {
            Row row = sheet.createRow(rowNumber++);
            cell = row.createCell(0);
            cell.setCellValue(statistics.getProducts().get(i));
            // 填充其他统计字段的数据
        }

        // 保存Excel文件
        try (FileOutputStream fos = new FileOutputStream(fileName)) {
            workbook.write(fos);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 销售记录类
    public static class SaleRecord {
        private String product;
        private String customer;
        private String region;
        private String store;
        private String date;

        // 省略getter和setter

        @Override
        public String toString() {
            return 'SaleRecord [product='' + product + '', customer='' + customer + '', region='' + region + '', store='' + store + '', date='' + date + '']';
        }
    }

    // 销售统计类
    public static class SalesStatistics {
        // 实现销售统计字段的定义和相应的getter和setter
        private List<String> products;

        public List<String> getProducts() {
            return products;
        }

        public void setProducts(List<String> products) {
            this.products = products;
        }
    }
}

注意:

  • 以上代码示例仅供参考,实际开发中需要根据具体业务需求进行修改和完善。
  • 需要引入 Apache POI 库来实现 Excel 文件的导出。
  • 需要根据实际数据库配置修改 DB_URLDB_USERDB_PASSWORD 等参数。
  • 需要实现 getYearlyStatisticsgetMonthlyStatisticsgetWeeklyStatisticsgetDailyStatistics 方法来计算相应的销售统计数据。
  • 需要根据具体统计需求完善 SalesStatistics 类,并添加相应的 getter 和 setter 方法。
  • 建议使用合适的框架和工具来简化开发,例如 Spring Boot、 MyBatis 等。

总结:

本系统实现了销售数据的统计分析功能,能够帮助公司更好地了解业务状况,提高决策效率。通过代码示例,可以了解系统的基本实现思路,并根据具体需求进行扩展和完善。

Java 销售数据分析系统:实现年、月、周、日统计和 Excel 导出

原文地址: https://www.cveoy.top/t/topic/hI8G 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录