|
public bool SaveExcelData(string path, DataTable table, bool bTitle)
{
bool result = true;
OleDbConnection conn = null;
try
{
OleDbCommand cmmd = null;
string connectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR=NO\"", path);
conn = new OleDbConnection(connectString);
conn.Open();
// 테이블 생성
StringBuilder sQryField = new StringBuilder();
StringBuilder sQryValue = new StringBuilder();
int cnt = 0;
foreach (DataColumn column in table.Columns)
{
DataRow row = null;
if (table.Rows.Count > 0) row = table.Rows[0];
if (sQryField.Length > 0) sQryField.Append(",");
sQryField.Append("[" + column.ColumnName.Replace("'", "''") + "] CHAR(255)");
}
cmmd = new OleDbCommand("CREATE TABLE Sheet1 (" + sQryField.ToString() + ")", conn);
cmmd.ExecuteNonQuery();
// 데이터 넣기
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
sQryField = new StringBuilder();
sQryValue = new StringBuilder();
cnt = 0;
foreach (DataColumn column in table.Columns)
{
if (sQryField.Length > 0)
{
sQryField.Append(",");
sQryValue.Append(",");
}
sQryField.Append("F" + (++cnt).ToString());
sQryValue.Append("'" + row[column.ColumnName].ToString().Replace("'", "''") + "'");
}
cmmd = new OleDbCommand("INSERT INTO [Sheet1$] (" + sQryField.ToString() + ") VALUES(" + sQryValue.ToString() + ")", conn);
cmmd.ExecuteNonQuery();
}
}
catch (Exception)
{
result = false;
}
finally
{
if (conn != null) conn.Close();
}
return result;
}
public bool SaveExcelData(string path, DataTable table, bool bTitle)
{
bool result = true;
OleDbConnection conn = null;
try
{
OleDbCommand cmmd = null;
string connectString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"Excel 8.0;HDR=NO\"", path);
conn = new OleDbConnection(connectString);
conn.Open();
// 테이블 생성
StringBuilder sQryField = new StringBuilder();
StringBuilder sQryValue = new StringBuilder();
int cnt = 0;
foreach (DataColumn column in table.Columns)
{
DataRow row = null;
if (table.Rows.Count > 0) row = table.Rows[0];
if (sQryField.Length > 0) sQryField.Append(",");
sQryField.Append("[" + column.ColumnName.Replace("'", "''") + "] CHAR(255)");
}
cmmd = new OleDbCommand("CREATE TABLE Sheet1 (" + sQryField.ToString() + ")", conn);
cmmd.ExecuteNonQuery();
// 데이터 넣기
for (int i = 0; i < table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
sQryField = new StringBuilder();
sQryValue = new StringBuilder();
cnt = 0;
foreach (DataColumn column in table.Columns)
{
if (sQryField.Length > 0)
{
sQryField.Append(",");
sQryValue.Append(",");
}
sQryField.Append("F" + (++cnt).ToString());
sQryValue.Append("'" + row[column.ColumnName].ToString().Replace("'", "''") + "'");
}
cmmd = new OleDbCommand("INSERT INTO [Sheet1$] (" + sQryField.ToString() + ") VALUES(" + sQryValue.ToString() + ")", conn);
cmmd.ExecuteNonQuery();
}
}
catch (Exception)
{
result = false;
}
finally
{
if (conn != null) conn.Close();
}
return result;
}
.NET 소스 긁어오기 / .NET 소스 긁어오기 Fetch (0) | 2019.05.30 |
---|---|
C# 다른 프로그램 실행여부 확인하기 (0) | 2019.03.14 |
c# 응용프로그램에서 txt에 있는 파일 내용 가져와서 응용 (0) | 2019.03.12 |
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |