diff --git a/WinFormsApp1/Form1.Designer.cs b/WinFormsApp1/Form1.Designer.cs index 0ddc6d7..c0cbd98 100644 --- a/WinFormsApp1/Form1.Designer.cs +++ b/WinFormsApp1/Form1.Designer.cs @@ -2,7 +2,7 @@ using System.Windows.Forms; using System.Drawing; -namespace WinFormsApp1 +namespace WinFormsAppTemplate { partial class Form1 { @@ -37,7 +37,7 @@ namespace WinFormsApp1 // 主窗体设置 AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(600, 400); - Text = "Excel合并工具"; + Text = "WinForms应用模板"; StartPosition = FormStartPosition.CenterScreen; // 创建控件 @@ -61,7 +61,7 @@ namespace WinFormsApp1 txtLog = new TextBox(); // 设置源文件夹组 - grpSourceFolder.Text = "源文件夹"; + grpSourceFolder.Text = "输入文件夹"; grpSourceFolder.Location = new Point(10, 10); grpSourceFolder.Size = new Size(580, 60); grpSourceFolder.TabStop = false; @@ -130,7 +130,7 @@ namespace WinFormsApp1 pnlButtons.Location = new Point(10, 150); pnlButtons.Size = new Size(580, 40); - btnMerge.Text = "合并"; + btnMerge.Text = "执行"; btnMerge.Location = new Point(10, 8); btnMerge.Size = new Size(75, 25); btnMerge.Click += new EventHandler(btnMerge_Click); diff --git a/WinFormsApp1/Form1.cs b/WinFormsApp1/Form1.cs index dfe0291..0ceae36 100644 --- a/WinFormsApp1/Form1.cs +++ b/WinFormsApp1/Form1.cs @@ -1,11 +1,7 @@ using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; using System.Windows.Forms; -using System.Data; -namespace WinFormsApp1 +namespace WinFormsAppTemplate { public partial class Form1 : Form { @@ -28,12 +24,11 @@ namespace WinFormsApp1 { using (FolderBrowserDialog dialog = new FolderBrowserDialog()) { - dialog.Description = "选择Excel文件夹"; + dialog.Description = "选择文件夹"; if (dialog.ShowDialog() == DialogResult.OK) { txtSourcePath.Text = dialog.SelectedPath; - txtOutputPath.Text = dialog.SelectedPath; // 默认输出到源文件夹 - Log($"源文件夹: {dialog.SelectedPath}"); + Log($"选择的文件夹: {dialog.SelectedPath}"); } } } @@ -66,192 +61,8 @@ namespace WinFormsApp1 private void btnMerge_Click(object sender, EventArgs e) { - // 前置校验 - if (string.IsNullOrEmpty(txtSourcePath.Text) || string.IsNullOrEmpty(txtOutputPath.Text)) - { - MessageBox.Show("请选择源文件夹和输出文件夹", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - try - { - MergeExcel(); - } - catch (Exception ex) - { - MessageBox.Show($"合并过程中发生错误: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - Log($"合并失败: {ex.Message}"); - } - } - - private void MergeExcel() - { - var sourceDir = txtSourcePath.Text; - var outputDir = txtOutputPath.Text; - var outputFile = Path.Combine(outputDir, "合并结果.xlsx"); - - if (!Directory.Exists(sourceDir)) - { - MessageBox.Show("源文件夹不存在", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - // 查找xlsx文件(排除合并结果本身) - var xlsxFiles = Directory.GetFiles(sourceDir, "*.xlsx") - .Where(f => !Path.GetFileName(f).Equals("合并结果.xlsx", StringComparison.OrdinalIgnoreCase)) - .ToArray(); - - if (xlsxFiles.Length == 0) - { - MessageBox.Show("未找到待合并的Excel文件", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - Log($"找到 {xlsxFiles.Length} 个文件,开始合并..."); - progressBar.Maximum = xlsxFiles.Length; - progressBar.Value = 0; - - - var allData = new List(); - var headers = new List(); - - try - { - // 处理每个文件 - for (int idx = 0; idx < xlsxFiles.Length; idx++) - { - var file = xlsxFiles[idx]; - var fileName = Path.GetFileName(file); - - Log($"处理: {fileName}"); - - try - { - using (var package = new OfficeOpenXml.ExcelPackage(new FileInfo(file))) - { - if (package.Workbook.Worksheets.Count == 0) - { - Log($"跳过空文件: {fileName}"); - continue; - } - - var worksheet = package.Workbook.Worksheets[0]; // 使用第一个工作表 - var table = new DataTable(); - - // 获取表头 - if (headers.Count == 0) - { - var columnCount = worksheet.Dimension.End.Column; - for (int col = 1; col <= columnCount; col++) - { - var header = worksheet.Cells[1, col].Text?.Trim() ?? $"Column{col}"; - headers.Add(header); - table.Columns.Add(header); - } - } - else - { - // 创建相同结构的表 - foreach (var header in headers) - { - table.Columns.Add(header); - } - } - - // 跳过表头行,从第二行开始读取数据 - var rowCount = worksheet.Dimension.End.Row; - for (int row = 2; row <= rowCount; row++) - { - var dataRow = table.NewRow(); - var isEmpty = true; - - for (int col = 0; col < headers.Count; col++) - { - var cellValue = worksheet.Cells[row, col + 1].Text; - dataRow[col] = cellValue; - if (!string.IsNullOrWhiteSpace(cellValue)) - { - isEmpty = false; - } - } - - if (!isEmpty) - { - table.Rows.Add(dataRow); - } - } - - if (table.Rows.Count > 0) - { - allData.Add(table); - Log($"成功读取 {fileName}: {table.Rows.Count} 行数据"); - } - else - { - Log($"跳过无数据文件: {fileName}"); - } - } - } - catch (Exception ex) - { - Log($"处理失败: {fileName} - {ex.Message}"); - continue; - } - - progressBar.Value = idx + 1; - Application.DoEvents(); - } - - // 合并数据并保存 - if (allData.Count == 0) - { - MessageBox.Show("无有效数据可合并", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - - // 创建合并后的Excel文件 - using (var outputPackage = new OfficeOpenXml.ExcelPackage()) - { - var outputSheet = outputPackage.Workbook.Worksheets.Add("合并结果"); - - // 写入表头 - for (int col = 0; col < headers.Count; col++) - { - outputSheet.Cells[1, col + 1].Value = headers[col]; - } - - int currentRow = 2; - int totalRows = 0; - - // 合并所有数据 - foreach (var table in allData) - { - foreach (DataRow row in table.Rows) - { - for (int col = 0; col < headers.Count; col++) - { - outputSheet.Cells[currentRow, col + 1].Value = row[col]; - } - currentRow++; - totalRows++; - } - } - - // 保存文件 - outputPackage.SaveAs(new FileInfo(outputFile)); - - Log($"合并完成!文件路径: {outputFile}"); - Log($"总行数: {totalRows} | 总列数: {headers.Count}"); - - MessageBox.Show($"合并成功!\n文件:{outputFile}\n行数:{totalRows}", - "完成", MessageBoxButtons.OK, MessageBoxIcon.Information); - } - } - catch (Exception ex) - { - Log($"保存失败: {ex.Message}"); - MessageBox.Show($"保存文件失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); - } + MessageBox.Show("这是一个模板项目\n\n功能:\n• 单exe打包\n• 应用图标配置\n• UI按钮图标\n• 环境检测\n\n请在此基础上开发你的具体功能", "模板项目", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log("模板功能演示"); } private void Log(string msg) diff --git a/WinFormsApp1/Program.cs b/WinFormsApp1/Program.cs index d7d2fb3..caeed1e 100644 --- a/WinFormsApp1/Program.cs +++ b/WinFormsApp1/Program.cs @@ -2,7 +2,7 @@ using System; using System.Windows.Forms; using Microsoft.Win32; -namespace WinFormsApp1 +namespace WinFormsAppTemplate { internal static class Program { diff --git a/WinFormsApp1/WinFormsApp1.csproj b/WinFormsApp1/WinFormsApp1.csproj index 4e4bbbb..49c7c39 100644 --- a/WinFormsApp1/WinFormsApp1.csproj +++ b/WinFormsApp1/WinFormsApp1.csproj @@ -4,8 +4,8 @@ WinExe net48 true - ExcelMerger - ExcelMerger + WinFormsAppTemplate + WinFormsAppTemplate app.ico false @@ -15,25 +15,22 @@ true + + + - - + - - - - + --> \ No newline at end of file