/// <summary>
 /// 初始化级别
 /// </summary>
 private void dataSource_labels()
 {
     this.combobox_labels.DataSource    = DbUtil.queryButtons("select * from t_labels order by id");
     this.combobox_labels.DisplayMember = "name";
 }
 /// <summary>
 /// 初始化明细grid
 /// </summary>
 private void dataSource_weights()
 {
     this.grid_weights.DataSource = DbUtil.queryWeights("select w.*, (@i:=@i+1) i from t_weights w,(select @i:=0) t2 where life_cycle=1 and is_upload=0 and DATE(create_time) = '" + this.input_date.Value.ToString("yyyy-MM-dd") + "' order by sn;");
     this.grid_weights.ClearSelection();
 }
Beispiel #3
0
        private void ExportExcels(string fileName, DataGridView myDGV)
        {
            string         saveFileName = "";
            SaveFileDialog saveDialog   = new SaveFileDialog();

            saveDialog.DefaultExt = "xls";
            saveDialog.Filter     = "Excel文件|*.xls";
            saveDialog.FileName   = fileName;
            saveDialog.ShowDialog();
            saveFileName = saveDialog.FileName;
            if (saveFileName.IndexOf(":") < 0)
            {
                return; //被点了取消
            }
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return;
            }
            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
            Microsoft.Office.Interop.Excel.Workbook  workbook  = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
            Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];                                                            //取得sheet1
            List <Weights> list = DbUtil.queryWeights("select w.*, (@i:=@i+1) i from t_weights w,(select @i:=0) t2 where life_cycle=1 and DATE(create_time) = '" + today + "' order by sn;"); //写入标题

            worksheet.Cells[1, 1] = "序号";
            worksheet.Cells[1, 2] = "顺序号";
            worksheet.Cells[1, 3] = "时间";
            worksheet.Cells[1, 4] = "重量";
            worksheet.Cells[1, 5] = "级别";
            worksheet.Cells[1, 6] = "备注";
            worksheet.Cells[1, 7] = "工艺";
            worksheet.Cells[1, 8] = "上传";
            worksheet.Cells[1, 9] = "补录";
            for (int r = 0; r < list.Count; r++)
            {
                worksheet.Cells[r + 2, 1] = list[r].row_num;
                worksheet.Cells[r + 2, 2] = list[r].sn;
                worksheet.Cells[r + 2, 3] = list[r].create_time;
                worksheet.Cells[r + 2, 4] = list[r].weight;
                worksheet.Cells[r + 2, 5] = list[r].level;
                worksheet.Cells[r + 2, 6] = list[r].remarks;
                worksheet.Cells[r + 2, 7] = list[r].type;
                worksheet.Cells[r + 2, 8] = list[r].is_upload;
                worksheet.Cells[r + 2, 9] = list[r].is_handwrite;
            }
            //for (int i = 0; i < myDGV.ColumnCount; i++)
            //{
            //    if (myDGV.Columns[i].Visible)
            //    {
            //        worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText;
            //    }
            //}
            ////写入数值
            //for (int r = 0; r < myDGV.Rows.Count; r++)
            //{
            //    for (int i = 0; i < myDGV.ColumnCount; i++)
            //    {
            //        if (myDGV.Rows[r].Cells[i].Visible)
            //        {
            //            worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value;
            //        }
            //    }
            //    System.Windows.Forms.Application.DoEvents();
            //}
            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            if (saveFileName != "")
            {
                try
                {
                    workbook.Saved = true;
                    workbook.SaveCopyAs(saveFileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                }
            }
            xlApp.Quit();
            MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }