111,129
社区成员
发帖
与我相关
我的任务
分享
using System.Data.OleDb;
#region ExportToMultipleExcelSheets
private static System.Text.StringBuilder SqlScript = new System.Text.StringBuilder();
private static System.Text.StringBuilder SqlInsert = new System.Text.StringBuilder();
private static bool mPredefineFile = false;
public static void ImportToMultipleXLSheets(System.String SqlSelect,System.String samplePath, System.String mOutputFileName,System.Data.DataSet ds)
{
string FolderPath;
FolderPath = mOutputFileName.Remove(mOutputFileName.LastIndexOf("\\"), mOutputFileName.Length - mOutputFileName.LastIndexOf("\\"));
//File.Copy(FolderPath + "\\Sample.xls", mOutputFileName, true);
File.Copy(samplePath, mOutputFileName, true);
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mOutputFileName + ";Extended Properties='Excel 8.0'";
OleDbConnection xlConn = new OleDbConnection(connstr);
try
{
xlConn.Open();
PrepareScript(ds.Tables[0]);
StartImport(ds.Tables[0], xlConn);
}
catch (Exception exp)
{
//throw new Exception("ImportToMultipleXLSheets", exp.InnerException);
throw new Exception(exp.Message, exp);
}
finally
{
if (xlConn != null)
{
if (xlConn.State == ConnectionState.Open) xlConn.Close();
xlConn.Dispose();
}
}
}
private static void CreateXLSheets(DataTable DTable, OleDbConnection xlConn, System.String XLSheetName)
{
// Create New Excel Sheet
System.Text.StringBuilder SqlFinalScript = new System.Text.StringBuilder();
OleDbCommand cmdXl = new OleDbCommand();
try
{
SqlFinalScript.Length = 0;
cmdXl.Connection = xlConn;
SqlFinalScript.Append("CREATE TABLE [" + XLSheetName + "$] (");
SqlFinalScript.Append(SqlScript.ToString());
cmdXl.CommandText = SqlFinalScript.ToString();
cmdXl.ExecuteNonQuery();
}
catch (Exception xlSheetExp)
{
//throw (new Exception("CreateXLSheetException", xlSheetExp.InnerException));
throw new Exception(xlSheetExp.Message, xlSheetExp);
}
finally
{
cmdXl.Dispose();
}
}
private static string PrepareScript(DataTable DTable)
{
// Prepare Scripts to create excel Sheet
SqlInsert.Length = 0;
SqlScript.Length = 0;
for (int i = 0; i < DTable.Columns.Count; i++)
{
SqlInsert.Append("[" + DTable.Columns[i].ColumnName + "],");
SqlScript.Append("[" + DTable.Columns[i].ColumnName.Replace("'", "''") + "]");
if (DTable.Columns[i].DataType.ToString().ToLower().Contains("int") || DTable.Columns[i].DataType.ToString().ToLower().Contains("decimal"))
SqlScript.Append(" double");
else
SqlScript.Append(" text");
SqlScript.Append(", ");
}
SqlInsert.Remove(SqlInsert.Length - 1, 1);
SqlScript.Remove(SqlScript.Length - 2, 1);
SqlScript.Append(") ");
return SqlScript.ToString();
}
private static void StartImport(DataTable DTable, OleDbConnection xlConn)
{
Int64 rowNo = 0, xlSheetIndex = 2, TotalNoOfRecords = 0;
System.String NewXLSheetName = "Sheet";
System.Text.StringBuilder strInsert = new System.Text.StringBuilder();
TotalNoOfRecords = DTable.Rows.Count;
OleDbCommand cmdXl = new OleDbCommand();
cmdXl.Connection = xlConn;
if (mPredefineFile) xlSheetIndex = 1;
xlSheetIndex = 1;
for (int count = 0; count < DTable.Rows.Count; count++)
{
strInsert.Length = 0;
if (rowNo == 0 && !mPredefineFile)
CreateXLSheets(DTable, xlConn, NewXLSheetName + xlSheetIndex);
rowNo += 1;
// TotalNoOfRecords : Total no of records return by Sql Query, ideally should be set to 65535
//rowNo : current Row no in the loop
if (TotalNoOfRecords > 5000 && rowNo > 5000)
{
xlSheetIndex += 1;
if (!mPredefineFile) CreateXLSheets(DTable, xlConn, NewXLSheetName + xlSheetIndex);
rowNo = 1;
}
strInsert.Append("Insert Into [" + NewXLSheetName + xlSheetIndex.ToString() + "$](" + SqlInsert.ToString() + ") Values (");
foreach (DataColumn dCol in DTable.Columns)
{
if (dCol.DataType.ToString().ToLower().Contains("int"))
{
if (DTable.Rows[count][dCol.Ordinal].ToString() == "")
strInsert.Append("NULL");
else
strInsert.Append(DTable.Rows[count][dCol.Ordinal]);
}
else if (dCol.DataType.ToString().ToLower().ToLower().Contains("decimal"))
{
if (DTable.Rows[count][dCol.Ordinal].ToString() == "")
strInsert.Append("NULL");
else
strInsert.Append(DTable.Rows[count][dCol.Ordinal]);
}
else
strInsert.Append("\"" + DTable.Rows[count][dCol.Ordinal].ToString().Replace("'", "''") + "\"");
strInsert.Append(",");
}
strInsert.Remove(strInsert.Length - 1, 1);
strInsert.Append(");");
cmdXl.CommandText = strInsert.ToString();
cmdXl.ExecuteNonQuery();
}
}
#endregion