/// <summary> /// 加载运单主体信息 /// </summary> public void waybillbaseLoad(string waybillNumber, string pageIndexAndCount, string startTime, string endTime, int senderId, int receiverId) { try { if (startTime != null && endTime != null) { if (Convert.ToDateTime(startTime) > Convert.ToDateTime(endTime)) { MessageBox.Show("开始时间不能大于结束时间"); return; } } ResultModelOfint result = cs.GetQueryClientsListCount(waybillNumber, startTime, endTime, senderId, true, receiverId, true); winFormPager1.DrawControl(result.Data); if (result.Data <= 0) { contextMenuStrip1.Enabled = false; } else { contextMenuStrip1.Enabled = true; } ResultModelOfArrayOfModel_Waybill_Based4FqxSXX waybill = cs.GetQueryClientsList(waybillNumber, pageIndexAndCount, startTime, endTime, senderId, true, receiverId, true); if (waybill.Code != 0) { MessageBox.Show(waybill.Message); } else { dataGridView1.AutoGenerateColumns = false; dataGridView1.Rows.Clear(); foreach (Model_Waybill_Base item in waybill.Data) { int rowIndex = dataGridView1.Rows.Add(); dataGridView1.Rows[rowIndex].Cells[0].Value = item.Numberk__BackingField; dataGridView1.Rows[rowIndex].Cells[1].Value = item.ReceiverOrgk__BackingField; dataGridView1.Rows[rowIndex].Cells[2].Value = item.ReceiverPersonk__BackingField; dataGridView1.Rows[rowIndex].Cells[3].Value = item.ReceiverTelk__BackingField; dataGridView1.Rows[rowIndex].Cells[4].Value = item.ReceiverAddressk__BackingField; dataGridView1.Rows[rowIndex].Cells[5].Value = item.BeginAtk__BackingField.ToString("yyyy-MM-dd HH:mm:ss"); dataGridView1.Rows[rowIndex].Tag = item; } } } catch (Exception ex) { MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void reportForm() { int rowCount = dataGridView1.Rows.Count; //获取dataGridView1的行数 int columnsCount = dataGridView1.Columns.Count; //获取dataGridView1的列数 //判断是否存在值 if (rowCount == 0) { MessageBox.Show("没有数据可供导出。。。", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } //定义个变量接收文件名字 string fileName = string.Empty; //new 一个对象用于提示用户选择保存文件位置 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "导出Excel (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; //筛选器的索引 saveFileDialog.RestoreDirectory = true; //对话框关闭前是否还原当前目录 saveFileDialog.CreatePrompt = true; //指定文件不存在,提示用户是否允许创建该文件 saveFileDialog.Title = "导出文件保存路径"; //设置对话框标题 //当前时间 DateTime now = DateTime.Now; saveFileDialog.FileName = now.Year.ToString().PadLeft(2) + now.Month.ToString().PadLeft(2, '0') + now.Day.ToString().PadLeft(2, '0') + "-" + now.Hour.ToString().PadLeft(2, '0') + now.Minute.ToString().PadLeft(2, '0') + now.Second.ToString().PadLeft(2, '0'); if (saveFileDialog.ShowDialog() == DialogResult.OK) { fileName = saveFileDialog.FileName; Missing miss = Missing.Value; Excel.Application excel = new Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。 if (excel == null) { MessageBox.Show("Excel无法启动", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Workbooks workbooks = excel.Workbooks; Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet worksheet = (Worksheet)workbook.Worksheets[1]; //取得sheet1 //写入标题 for (int i = 0; i < dataGridView1.ColumnCount; i++) { worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText; } #region 导出一页 ////写入数值 //for (int r = 0; r < dataGridView1.Rows.Count; r++) //{ // for (int i = 0; i < dataGridView1.ColumnCount; i++) // { // worksheet.Cells[r + 2, i + 1] = dataGridView1.Rows[r].Cells[i].Value; // } // System.Windows.Forms.Application.DoEvents(); //} #endregion ResultModelOfArrayOfModel_Waybill_Based4FqxSXX waybill = cs.GetWaybillList(waybillNumber, null, startTime, endTime, customerId, true, 3, true); if (waybill.Code != 0) { MessageBox.Show(waybill.Message); } else { //写入数值 for (int r = 0; r < waybill.Data.Length; r++) { //在字段前加单引号是为了防止自动转化格式,如057410007009导出后转化格式前面的0不会消失 worksheet.Cells[r + 2, 1] = "'" + waybill.Data[r].Numberk__BackingField; worksheet.Cells[r + 2, 2] = waybill.Data[r].ReceiverOrgk__BackingField; worksheet.Cells[r + 2, 3] = waybill.Data[r].ReceiverPersonk__BackingField; worksheet.Cells[r + 2, 4] = waybill.Data[r].ReceiverTelk__BackingField; worksheet.Cells[r + 2, 5] = waybill.Data[r].ReceiverAddressk__BackingField; worksheet.Cells[r + 2, 6] = waybill.Data[r].BeginAtk__BackingField; worksheet.Cells[r + 2, 7] = waybill.Data[r].BillingCountk__BackingField; System.Windows.Forms.Application.DoEvents(); } } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 if (fileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(fileName); } catch (Exception ex) { MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message); } } excel.Quit(); GC.Collect();//强行销毁 MessageBox.Show(fileName + "的简明资料保存成功", "提示", MessageBoxButtons.OK); } }