我花了一些时间才找到从数据库中选择数据的方法,然后将其粘贴到Excel电子表格中。现在我已经发现了它,它的运行速度很慢。就像我之前说过的那样,我已经看了很多不同的方法来完成这个但是除了这个之外还没能正确地实现它们中的任何一个。我没有和这个选项结婚,但这是我唯一可以上班的人。有人可以通过建议更快的方式完成这个简单的任务来帮助我吗?请参阅下面的代码。
Record850 rec850 = new Record850();
List<Record850> lst850records = new List<Record850>();
//SqlConnection connStr = new SqlConnection("Server = 172.18.211.76; Database = Processstage; User Id = brendon.davies; Password = mypassword;");
SqlConnection conn = new SqlConnection("Server = 172.18.211.76; Database = Processstage; User Id = brendon.davies; Password = mypassword;");
//SqlConnection sqlConnection1 = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = Select_850;
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
conn.Open();
Record850 reco850 = new Record850();
string strComplete = "";
reader = cmd.ExecuteReader();
while (reader.Read())
{
try
{
reco850.OrgName = reader.GetString(0);
reco850.WholeSalerAccountDivisionCode = reader.GetString(1);
reco850.File_Process_Name = reader.GetString(2);
reco850.Pur_Ord_Num_BEG03 = reader.GetString(3);
reco850.File_Process_ID = reader.GetInt64(4);
reco850.CECode = reader.GetString(5);
reco850.CEName = reader.GetString(6);
reco850.Modified_Date = reader.GetDateTime(7);
lst850records.Add(reco850);
reco850 = new Record850();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
conn.Close();
eWorkSheet = (Excel.Worksheet)oSheets.get_Item("850_Template");
eWorkSheet.Activate();
int int850counter = 0;
int int850RowCounter = 3;
foreach (Record850 r850 in lst850records)
{
strComplete = lst850records[int850counter].OrgName + "\t" +
lst850records[int850counter].WholeSalerAccountDivisionCode + "\t" +
lst850records[int850counter].File_Process_Name + "\t" +
lst850records[int850counter].Pur_Ord_Num_BEG03 + "\t" +
lst850records[int850counter].File_Process_ID + "\t" +
lst850records[int850counter].CECode + "\t" +
lst850records[int850counter].CEName+ "\t" +
lst850records[int850counter].Modified_Date;
CR = (Excel.Range)eWorkSheet.Cells[int850RowCounter,3];
Clipboard.SetText(strComplete);
CR.Select();
eWorkSheet.Paste(CR, false);
Clipboard.Clear();
int850RowCounter++;
int850counter++;
strComplete = ""
}
答案 0 :(得分:1)
这是一种使用EPPlus方法的替代方案,速度更快。
try {
var app = new ExcelPackage(new FileInfo(SOURCE_PATH));
var ws = app.Workbook.Worksheets["850_Template"];
int row = 3;
using (SqlConnection cn = new SqlConnection(CN_STR)) {
cn.Open();
using (SqlCommand cmd = new SqlCommand(SQL, cn)) {
cmd.CommandType = CommandType.Text;
using (SqlDataReader dr = cmd.ExecuteReader()) {
while (dr.Read())
{
ws.SetValue(row, 1, (string) dr["OrgName"]);
ws.SetValue(row, 2, (string) dr["WholeSalerAccountDivisionCode"]);
ws.SetValue(row, 3, (string) dr["File_Process_Name"]);
ws.SetValue(row, 4, (string) dr["Pur_Ord_Num_BEG03"]);
ws.SetValue(row, 5, (long) dr["File_Process_ID"]);
ws.SetValue(row, 6, (string) dr["CECode"]);
ws.SetValue(row, 7, (string) dr["CEName"]);
ws.SetValue(row, 8, (DateTime) dr["Modified_Date"]);
row++;
}
}
}
cn.Close();
}
app.Save();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
}
答案 1 :(得分:1)
这里的答案也很快,与上面EPPlus的使用大致相同。 不尝试逐个单元格的方法(下面已注释),因为它确实以蜗牛的速度进行比较。
[STAThread]
static void Main(string[] args)
{
Excel.Application xl = null;
try {
xl = new Excel.Application();
xl.ScreenUpdating = false;
xl.Visible = false;
xl.UserControl = false;
var wb = xl.Workbooks.Open(SOURCE_PATH);
var ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets.Item["850_Template"];
StringBuilder sb = new StringBuilder();
//int row = 3;
// var a1 = ws.Range["A1", Missing.Value];
using (SqlConnection cn = new SqlConnection(CN_STR)) {
cn.Open();
using (SqlCommand cmd = new SqlCommand(SQL, cn)) {
cmd.CommandType = CommandType.Text;
using (SqlDataReader dr = cmd.ExecuteReader()) {
while (dr.Read())
{
sb.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n",
(string)dr["OrgName"],
(string)dr["WholeSalerAccountDivisionCode"],
(string)dr["File_Process_Name"],
(string)dr["Pur_Ord_Num_BEG03"],
(long)dr["File_Process_ID"],
(string)dr["CECode"],
(string)dr["CEName"],
(DateTime)dr["Modified_Date"]
);
//a1.Offset[row, 0].Value = (string)dr["OrgName"];
//a1.Offset[row, 1].Value = (string)dr["WholeSalerAccountDivisionCode"];
//a1.Offset[row, 2].Value = (string)dr["File_Process_Name"];
//a1.Offset[row, 3].Value = (string)dr["Pur_Ord_Num_BEG03"];
//a1.Offset[row, 4].Value = (long)dr["File_Process_ID"];
//a1.Offset[row, 5].Value = (string)dr["CECode"];
//a1.Offset[row, 6].Value = (string)dr["CEName"];
//a1.Offset[row, 7].Value = (DateTime)dr["Modified_Date"];
//row++;
}
}
}
cn.Close();
}
Clipboard.SetText(sb.ToString(),
TextDataFormat.Text);
var rng = ws.Range["A3", Missing.Value];
rng.Select();
ws.Paste(rng, Missing.Value);
Clipboard.Clear();
wb.Save();
wb.Close();
xl.Quit();
} catch (Exception ex) {
Console.WriteLine(ex.Message);
if (xl != null) {
xl.ScreenUpdating = true;
xl.Visible = true;
xl.UserControl = true;
}
}
}