#region 事件 导出Excel表格 客户基本信息
private void btn_Export_Click(object sender, EventArgs e)
{
if(this.dgv_CustomerInfo.Rows.Count<1)
{
MessageBox.Show("没有要导出的客户信息!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
StatesBar sb = new StatesBar();
try
{
saveFileDialog1.Title = "不凡帝订单系统导出----荣庆物流";
saveFileDialog1.Filter = "Excel(*.xls)|*.xls";
saveFileDialog1.FileName = string.Format("客户基本信息--{0}", DateTime.Now.ToString("yyyyMMdd"));
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
sb.Show("系统正在处理中...请稍候!", true);
Microsoft.Office.Interop.Excel._Application xlapp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook xlbook = xlapp.Workbooks.Add(true);
Microsoft.Office.Interop.Excel.Worksheet xlsheet = (Microsoft.Office.Interop.Excel.Worksheet)xlbook.Worksheets[1];
int colIndex = 0;
int RowIndex = 1;
string headInfo = "客户名称,客户编码,办事处,承运商,省份,城市,收货地址,收货人,联系电话,备注,录入时间";
//开始写入每列的标题
foreach (var s in headInfo.Split(','))
{
colIndex++;
xlsheet.Cells[RowIndex, colIndex] = s;
}
//开始写入内容
int RowCount = this.dgv_CustomerInfo.Rows.Count;//行数
for (int i = 0; i < RowCount; i++)
{
RowIndex++;
xlsheet.Cells[RowIndex, 1].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 1] = dgv_CustomerInfo.Rows[i].Cells["CustomerName"].Value.ToString();
xlsheet.Cells[RowIndex, 2].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 2] = dgv_CustomerInfo.Rows[i].Cells["CustomerCode"].Value.ToString();
xlsheet.Cells[RowIndex, 3].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 3] = dgv_CustomerInfo.Rows[i].Cells["DepartmentName"].Value.ToString();
xlsheet.Cells[RowIndex, 4].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 4] = dgv_CustomerInfo.Rows[i].Cells["CarrierName"].Value.ToString();
xlsheet.Cells[RowIndex, 5].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 5] = dgv_CustomerInfo.Rows[i].Cells["SendProvinceName"].Value.ToString();
xlsheet.Cells[RowIndex, 6].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 6] = dgv_CustomerInfo.Rows[i].Cells["SendCityName"].Value.ToString();
xlsheet.Cells[RowIndex, 7].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 7] = dgv_CustomerInfo.Rows[i].Cells["ReceiveAddress"].Value.ToString();
xlsheet.Cells[RowIndex, 8].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 8] = dgv_CustomerInfo.Rows[i].Cells["ReceiveName"].Value.ToString();
xlsheet.Cells[RowIndex, 9].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 9] = dgv_CustomerInfo.Rows[i].Cells["ReceiceLink"].Value.ToString();
xlsheet.Cells[RowIndex, 10].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 10] = dgv_CustomerInfo.Rows[i].Cells["Remark"].Value.ToString();
xlsheet.Cells[RowIndex, 11].NumberFormatLocal = "@";
xlsheet.Cells[RowIndex, 11] = dgv_CustomerInfo.Rows[i].Cells["WriteTime"].Value.ToString();
}
xlbook.Saved = true;
xlbook.SaveCopyAs(saveFileDialog1.FileName);
xlapp.Quit();
GC.Collect();
#region 强行杀死最近打开的Excel进程
System.Diagnostics.Process[] excelProc = System.Diagnostics.Process.GetProcessesByName("EXCEL");
System.DateTime startTime = new DateTime();
int m, killId = 0;
for (m = 0; m < excelProc.Length; m++)
{
if (startTime < excelProc[m].StartTime)
{
startTime = excelProc[m].StartTime;
killId = m;
}
}
if (excelProc[killId].HasExited == false)
{
excelProc[killId].Kill();
}
#endregion
sb.Close();
MessageBox.Show("导出成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch
{
sb.Close();
MessageBox.Show("导出失败!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
}