Files
JP_KDDI_LFPS_48100/LFP_Manager/Function/csExcelFunction.cs
2025-12-19 13:59:34 +09:00

146 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;
namespace LFP_Manager.Function
{
public class csExcelFunction
{
public static DataTable[] ExcelImport(string Ps_FileName, string[] sheetName)
{
DataTable[] result = null;
try
{
string ExcelConn = string.Empty;
if (Ps_FileName.IndexOf(".xlsx") > -1) // 확장자에 따라서 provider 주의
{
ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Ps_FileName
+ ";Extended Properties='Excel 12.0;HDR=YES'";
}
else
{
ExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Ps_FileName
+ ";Extended Properties='Excel 8.0;HDR=YES'";
}
// 첫 번째 시트의 이름을 가져옮
using (OleDbConnection con = new OleDbConnection(ExcelConn))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dtExcelSchema.Rows.Count > 0)
{
sheetName = new string[dtExcelSchema.Rows.Count];
for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
sheetName[i] = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
}
con.Close();
}
}
string msg = string.Empty;
for (int i = 0; i < sheetName.Length; i++)
msg += sheetName[i] + "\r\n";
//MessageBox.Show("sheetName = " + msg);
// 첫 번째 쉬트의 데이타를 읽어서 datagridview 에 보이게 함.
using (OleDbConnection con = new OleDbConnection(ExcelConn))
{
using (OleDbCommand cmd = new OleDbCommand())
{
using (OleDbDataAdapter oda = new OleDbDataAdapter())
{
result = new DataTable[sheetName.Length];
for (int i = 0; i < sheetName.Length; i++)
{
result[i] = new DataTable();
result[i].TableName = sheetName[i];
cmd.CommandText = "SELECT * From [" + sheetName[i] + "]";
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
try
{
oda.Fill(result[i]);
}
catch (Exception)
{
//MessageBox.Show(e1.Message);
}
con.Close();
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
public static void ExcelExport(string Ps_FileName, string[] sheetName)
{
string ExcelConn = string.Empty;
if (Ps_FileName.IndexOf(".xlsx") > -1) // 확장자에 따라서 provider 주의
{
ExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Ps_FileName
+ ";Extended Properties='Excel 12.0;HDR=YES;IMEX=3;READONLY=FALSE'";
}
else
{
ExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Ps_FileName
+ ";Extended Properties='Excel 8.0;HDR=YES;IMEX=3;READONLY=FALSE'";
}
// 첫 번째 시트의 이름을 가져옮
using (OleDbConnection con = new OleDbConnection(ExcelConn))
{
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = con;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dtExcelSchema.Rows.Count > 0)
{
sheetName = new string[dtExcelSchema.Rows.Count];
for (int i = 0; i < dtExcelSchema.Rows.Count; i++)
sheetName[i] = dtExcelSchema.Rows[i]["TABLE_NAME"].ToString();
}
con.Close();
}
}
using (OleDbConnection connection =
new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"C:\Users\[...]\Classeur.xls"
+ ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1;READONLY=FALSE\""))
{
connection.Open();
OleDbCommand commande = new OleDbCommand(
"INSERT INTO [Feuil1$](F1,F2,F3) VALUES ('A3','B3','C3');", connection);
commande.ExecuteNonQuery();
connection.Close();
connection.Dispose();
}
}
}
}