private void bt_bach_oracle_Click(object sender, EventArgs e)
{
//將下載的excel匯總為一個excel
string sourceFolder = @"D:\123"; // 源文件夹路径
string destinationFolder = @"D:\456"; // 目标文件夹路径
string destinationFile = "MergedFile.xlsx"; // 合并后的文件名
string sheetName = "123123"; // 工作表名
MergeExcelFiles(sourceFolder, destinationFolder, destinationFile);
//讀取excel
string filePath = @"D:\456\MergedFile.xlsx";
//DataTable dataTable = new DataTable();
// 加载Excel文件
using (var package = new ExcelPackage(new FileInfo(filePath)))
{
// 获取第一个工作表
//ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
// 遍历所有工作表
foreach (var worksheet in package.Workbook.Worksheets)
{
//dataTable = null;
DataTable dataTable = new DataTable();
// 遍历工作表的列以创建DataTable的列
int colCount = worksheet.Dimension.End.Column;
for (int col = 1; col <= colCount; col++)
{
dataTable.Columns.Add(worksheet.Cells[1, col].Value.ToString()); // 假设第一行是标题行
}
dataTable.Columns.Add("create_date", typeof(DateTime));//add一列,Create_date
// 遍历工作表的行以填充DataTable的行
int rowCount = worksheet.Dimension.End.Row;
for (int row = 2; row <= rowCount; row++) // 从第二行开始读取数据,假设第一行是标题行
{
DataRow newRow = dataTable.NewRow();
for (int col = 1; col <= colCount; col++)
{
newRow[col - 1] = worksheet.Cells[row, col].Value; // EPPlus中的索引是从1开始的,而DataTable中的索引是从0开始的
}
newRow[colCount] = DateTime.Now;//add一列,Create_date
dataTable.Rows.Add(newRow);
}
//dataGridView1.DataSource = dataTable;
//批量添加数据至数据库(**datatable结构要与数据库表结构一致**)
DBHelp.BulkCopyDataToOracle(DBHelp.ConnMES, "as160_pnl_pcs_test", dataTable);
}
}
//dataGridView1.DataSource = dataTable;
//批量insert數據庫
//DBHelp.BulkCopyDataToOracle(DBHelp.connect, "tabel_test", dataTable);
MessageBox.Show("excel數據批量上傳成功!!!");
}
// 多個excel整合成一個excel多個sheet
public static void MergeExcelFiles(string sourceFolderPath, string destinationFolderPath, string destinationFileName)
{
// 确保源文件夹存在
if (!Directory.Exists(sourceFolderPath))
{
throw new DirectoryNotFoundException("Source folder not found.");
}
// 确保目标文件夹存在,如果不存在则创建它
if (!Directory.Exists(destinationFolderPath))
{
Directory.CreateDirectory(destinationFolderPath);
}
// 获取源文件夹下所有的Excel文件
string[] excelFiles = Directory.GetFiles(sourceFolderPath, "*.xlsx", SearchOption.TopDirectoryOnly);
// 如果没有找到任何Excel文件,则退出
if (excelFiles.Length == 0)
{
Console.WriteLine("No Excel files found in the source folder.");
return;
}
// 创建一个新的ExcelPackage实例来保存合并后的数据
using (ExcelPackage package = new ExcelPackage())
{
// 遍历所有的Excel文件
foreach (string excelFile in excelFiles)
{
// 加载当前Excel文件
using (ExcelPackage sourcePackage = new ExcelPackage(new FileInfo(excelFile)))
{
// 遍历当前Excel文件中的所有工作表
foreach (ExcelWorksheet worksheet in sourcePackage.Workbook.Worksheets)
{
// 检查工作表名称是否已存在
string proposedName = worksheet.Name;
int i = 1;
while (package.Workbook.Worksheets.Any(ws => ws.Name == proposedName))
{
// 如果存在,添加一个唯一标识符(例如序号)
proposedName = $"{worksheet.Name}_{i++}";
}
// 添加或复制工作表到新的ExcelPackage(注意工作表名称的唯一性)
//ExcelWorksheet newWorksheet = package.Workbook.Worksheets.Add(worksheet.Name + "_" + Path.GetFileNameWithoutExtension(excelFile));
//ExcelWorksheet newWorksheet = package.Workbook.Worksheets.Add(Path.GetFileNameWithoutExtension(excelFile));//文件名命名Sheet名
ExcelWorksheet newWorksheet = package.Workbook.Worksheets.Add(proposedName);
// 如果需要复制数据,可以使用以下逻辑(这里只是示例,可能需要根据实际情况调整)
for (int row = 1; row <= worksheet.Dimension.End.Row; row++)
{
for (int col = 1; col <= worksheet.Dimension.End.Column; col++)
{
newWorksheet.Cells[row, col].Value = worksheet.Cells[row, col].Value;
}
}
// 这里只是简单地将工作表的结构复制到新工作表(不包含数据)
// 如果你需要复制数据,请取消注释上面的循环
// 如果你需要复制样式、格式等,你可能需要编写额外的逻辑来处理这些
}
}
}
// 保存合并后的Excel文件到目标文件夹
FileInfo newFile = new FileInfo(Path.Combine(destinationFolderPath, destinationFileName));
package.SaveAs(newFile);
}
Console.WriteLine("Excel files have been merged successfully.");
}
//数据库连接类class DBHelp
class DBHelp
{
public static string connect = "Host=你的IP;Port=端口;User ID=用户名;Password=密码;SID=数据库名";
//批量insert
public static void BulkCopyDataToOracle( string connectionString, string destinationTableName, DataTable dataTable)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
connection.Open();
using (OracleBulkCopy bulkCopy = new OracleBulkCopy(connection))
{
// 设置目标数据库表名
bulkCopy.DestinationTableName = destinationTableName;
// 將DataTable數據写入到目标表
bulkCopy.WriteToServer(dataTable);
}
}
}
}
标签:string,package,C#,excel,Workbook,oracle,new,DataTable From: https://www.cnblogs.com/xynmw/p/18324592