添加项目文件。
This commit is contained in:
243
口罩泄露定制款/Excle/ExperData_ExcleHelper.cs
Normal file
243
口罩泄露定制款/Excle/ExperData_ExcleHelper.cs
Normal file
@@ -0,0 +1,243 @@
|
||||
using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace 口罩泄露定制款.Excle
|
||||
{
|
||||
internal class ExperData_ExcleHelper
|
||||
{
|
||||
public void ExportToExcel(List<List<float>> data, List<string> timeList)
|
||||
{
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
// 创建 Excel 包
|
||||
using (var package = new ExcelPackage())
|
||||
{
|
||||
|
||||
|
||||
|
||||
// 添加一个工作表
|
||||
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
|
||||
|
||||
// 定义表头
|
||||
string[] headers = {"时间",
|
||||
"室内气溶胶浓度", "口罩内气溶胶浓度", "泄露率",
|
||||
"室内CO2浓度", "口罩内CO2浓度", "室内氧浓度", "室内温度", "室内湿度"
|
||||
};
|
||||
|
||||
// 写入表头
|
||||
for (int col = 0; col < headers.Length; col++)
|
||||
{
|
||||
worksheet.Cells[1, col + 1].Value = headers[col]; // 表头从第一行开始
|
||||
}
|
||||
//
|
||||
for (int i = 0; i < timeList.Count; i++)
|
||||
{
|
||||
worksheet.Cells[i + 2, 1].Value = timeList[i]; // 时间从第二行开始
|
||||
}
|
||||
|
||||
// 写入数据(从第二行开始)
|
||||
for (int col = 0; col < data.Count; col++)
|
||||
{
|
||||
List<float> columnData = data[col]; // 获取当前列的数据
|
||||
|
||||
for (int row = 0; row < columnData.Count; row++)
|
||||
{
|
||||
worksheet.Cells[row + 2, col + 2].Value = columnData[row]; // 数据从第二行开始
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 保存 Excel 文件
|
||||
SaveFileDialog saveFileDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel Files|*.xlsx",
|
||||
Title = "保存 Excel 文件"
|
||||
};
|
||||
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
|
||||
package.SaveAs(fileInfo);
|
||||
MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//public void ExportToExcel(List<List<List<float>>> data, List<List<string>> timeList, DataTable dataTable)
|
||||
//{
|
||||
// ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
// // 创建 Excel 包
|
||||
// using (var package = new ExcelPackage())
|
||||
// {
|
||||
// // 添加一个工作表
|
||||
// var worksheet1 = package.Workbook.Worksheets.Add("实验数据");
|
||||
|
||||
// // 写入表头
|
||||
// for (int col = 0; col < dataTable.Columns.Count; col++)
|
||||
// {
|
||||
// worksheet1.Cells[1, col + 1].Value = dataTable.Columns[col].ColumnName; // 表头从第一行开始
|
||||
// }
|
||||
|
||||
// // 写入数据
|
||||
// for (int row = 0; row < dataTable.Rows.Count; row++)
|
||||
// {
|
||||
// for (int col = 0; col < dataTable.Columns.Count; col++)
|
||||
// {
|
||||
// worksheet1.Cells[row + 2, col + 1].Value = dataTable.Rows[row][col]; // 数据从第二行开始
|
||||
// }
|
||||
// }
|
||||
|
||||
// // 遍历每个 List<List<float>>,生成一个 Sheet
|
||||
// for (int sheetIndex = 0; sheetIndex < data.Count; sheetIndex++)
|
||||
// {
|
||||
// // 添加一个工作表,命名为 1, 2, 3, ...
|
||||
// var worksheet = package.Workbook.Worksheets.Add((sheetIndex + 1).ToString());
|
||||
// // 定义表头
|
||||
// string[] headers = {"时间",
|
||||
// "室内气溶胶浓度", "口罩内气溶胶浓度", "泄露率",
|
||||
// "室内CO2浓度", "口罩内CO2浓度", "室内氧浓度", "室内温度", "室内湿度"
|
||||
// };
|
||||
// // 写入表头
|
||||
// for (int col = 0; col < headers.Length; col++)
|
||||
// {
|
||||
// worksheet.Cells[1, col + 1].Value = headers[col]; // 表头从第一行开始
|
||||
// }
|
||||
|
||||
// for (int i = 0; i < timeList.Count; i++)
|
||||
// {
|
||||
// List<string> timeList_Each = timeList[i]; // 获取当前列的数据
|
||||
// for (int j = 0; j < timeList_Each.Count; j++)
|
||||
// {
|
||||
// worksheet.Cells[j + 2, 1].Value = timeList_Each[j]; // 时间从第二行开始
|
||||
// }
|
||||
|
||||
// }
|
||||
// // 获取当前 Sheet 的数据
|
||||
// List<List<float>> sheetData = data[sheetIndex];
|
||||
|
||||
// // 写入数据
|
||||
// for (int row = 0; row < sheetData.Count; row++)
|
||||
// {
|
||||
// for (int col = 0; col < sheetData[row].Count; col++)
|
||||
// {
|
||||
// worksheet.Cells[row + 1, col + 2].Value = sheetData[row][col]; // 数据从第一行第一列开始
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// // 保存 Excel 文件
|
||||
// SaveFileDialog saveFileDialog = new SaveFileDialog
|
||||
// {
|
||||
// Filter = "Excel Files|*.xlsx",
|
||||
// Title = "保存 Excel 文件"
|
||||
// };
|
||||
|
||||
// if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
// {
|
||||
// FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
|
||||
// package.SaveAs(fileInfo);
|
||||
// MessageBox.Show("数据导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
// }
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
/// <summary>
|
||||
/// 所有实验数据导出EXCLE
|
||||
/// </summary>
|
||||
/// <param name="dataTable"></param>
|
||||
/// <param name="stringData"></param>
|
||||
/// <param name="floatData"></param>
|
||||
public void ExportToExcel(DataTable dataTable, List<List<string>> stringData, List<List<List<float>>> floatData)
|
||||
{
|
||||
// 创建 Excel 包
|
||||
using (var package = new ExcelPackage())
|
||||
{
|
||||
// 1. 将 DataTable 数据导入第一个工作表
|
||||
var firstWorksheet = package.Workbook.Worksheets.Add("DataTable");
|
||||
|
||||
// 写入表头
|
||||
for (int col = 0; col < dataTable.Columns.Count; col++)
|
||||
{
|
||||
firstWorksheet.Cells[1, col + 1].Value = dataTable.Columns[col].ColumnName;
|
||||
}
|
||||
|
||||
// 写入数据
|
||||
for (int row = 0; row < dataTable.Rows.Count; row++)
|
||||
{
|
||||
for (int col = 0; col < dataTable.Columns.Count; col++)
|
||||
{
|
||||
firstWorksheet.Cells[row + 2, col + 1].Value = dataTable.Rows[row][col];
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 根据 List<List<string>> 的 Count 生成同等数量的工作表
|
||||
for (int sheetIndex = 0; sheetIndex < stringData.Count; sheetIndex++)
|
||||
{
|
||||
// 添加工作表,命名为 1, 2, 3, ...
|
||||
var worksheet = package.Workbook.Worksheets.Add((sheetIndex + 1).ToString());
|
||||
//"姓名","性别","年龄","身份证号码","工作单位","工龄",
|
||||
// 定义表头
|
||||
string[] headers = {
|
||||
"时间", "室内气溶胶浓度", "口罩内气溶胶浓度", "泄露率",
|
||||
"室内CO2浓度", "口罩内CO2浓度", "室内氧浓度", "室内温度", "室内湿度"
|
||||
};
|
||||
|
||||
// 写入表头
|
||||
for (int col = 0; col < headers.Length; col++)
|
||||
{
|
||||
worksheet.Cells[1, col + 1].Value = headers[col];
|
||||
}
|
||||
|
||||
// 获取当前 Sheet 的字符串数据和浮点数数据
|
||||
List<string> currentStringData = stringData[sheetIndex];
|
||||
List<List<float>> currentFloatData = floatData[sheetIndex];
|
||||
|
||||
// 写入 List<List<string>> 数据到第一列
|
||||
for (int row = 0; row < currentStringData.Count; row++)
|
||||
{
|
||||
worksheet.Cells[row + 2, 1].Value = currentStringData[row];
|
||||
}
|
||||
|
||||
// 写入 List<List<List<float>>> 数据到第二列开始
|
||||
|
||||
// 写入数据(从第二行开始)
|
||||
for (int col = 0; col < currentFloatData.Count; col++)
|
||||
{
|
||||
List<float> columnData = currentFloatData[col]; // 获取当前列的数据
|
||||
|
||||
for (int row = 0; row < columnData.Count; row++)
|
||||
{
|
||||
worksheet.Cells[row + 2, col + 2].Value = columnData[row]; // 数据从第二行开始
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 保存 Excel 文件
|
||||
SaveFileDialog saveFileDialog = new SaveFileDialog
|
||||
{
|
||||
Filter = "Excel Files|*.xlsx",
|
||||
Title = "保存 Excel 文件"
|
||||
};
|
||||
|
||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
FileInfo fileInfo = new FileInfo(saveFileDialog.FileName);
|
||||
package.SaveAs(fileInfo);
|
||||
MessageBox.Show("数据导出成功!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//将excle数据导出到datatable
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user