`
收藏列表
标题 标签 来源
C# WPF openFileDialog的使用 上传Excel c# wpf openfiledialog
//一:浏览文件:(Excel) 
private void btnUpload_Click(object sender, RoutedEventArgs e)
        {
            //Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
            ofd.Filter = "Excel Files|*.xlsx;*.xls";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtPath.Text = ofd.FileName;
            }
            else {
                txtPath.Text = "";
            }
        }

//二:上传Excel到数据库
  string filename = txtPath.Text;
                        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                        Microsoft.Office.Interop.Excel.Workbook workbook;
                        Microsoft.Office.Interop.Excel.Worksheet worksheet;
                        object oMissing = System.Reflection.Missing.Value;//相当于null
                        workbook = excel.Workbooks.Open(filename, oMissing, oMissing, oMissing,oMissing,oMissing);
                        worksheet = (Worksheet)workbook.Worksheets[1];
                        int rowCount = worksheet.UsedRange.Rows.Count;
                        int colCount = worksheet.UsedRange.Columns.Count;
                        Microsoft.Office.Interop.Excel.Range range1;
                                                
                        for (int j = 1; j < rowCount; j++)
                        {
                            dutyInfo duty = new dutyInfo();
                            for (int i = 0; i < colCount; i++)
                            {
                                range1 = worksheet.Range[worksheet.Cells[j + 1, i + 1], worksheet.Cells[j + 1, i + 1]];
                                try
                                {
                                    if (range1 != null)
                                    {
                                        switch (i)
                                        {
                                            case 0://日期格式
                                                if (range1.Value2 != null) {
                                                    duty.dutyTime = DateTime.FromOADate(double.Parse(range1.Value2.ToString()));
                                                }
                                                break;
                                            case 1:
                                                if (range1.Value2 != null) {
                                                    duty.dutyLeader = range1.Value2.ToString();
                                                }
                                                break;
                                            case 2:
                                                if (range1.Value2 != null) {
                                                    duty.dutyMan = range1.Value2.ToString();
                                                }
                                                break;
                                            case 3:
                                                if (range1.Value2 != null) {
                                                    duty.weatherImg = range1.Value2.ToString();
                                                }
                                                break;
                                            case 4://图片格式
                                                range1.Select();
                                                //将单元格复制到剪贴板中  xlBitmap Bitmap (.bmp, .jpg, .gif).  xlPicture  picture (.png, .wmf, .mix). 
                                                range1.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);//COPY到内存 .bmp, .jpg, .gif
                                                //MessageBox.Show("copy 到 内存 bitmap" + System.Windows.Forms.Clipboard.ContainsImage());
                                                if (System.Windows.Forms.Clipboard.ContainsImage())
                                                {
                                                    System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
                                                    //保存到对象中
                                                    MemoryStream ms = new MemoryStream();
                                                    image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                                                    byte[] imgByte = ms.ToArray();
                                                    duty.leaderImg = imgByte;
                                                }
                                                else {
                                                    range1.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlPicture);//COPY到内存 .png, .wmf, .mix
                                                    //MessageBox.Show("copy 内存 Pic " + System.Windows.Forms.Clipboard.ContainsImage());
                                                    if (System.Windows.Forms.Clipboard.ContainsImage()) {
                                                        System.Drawing.Image image = System.Windows.Forms.Clipboard.GetImage();
                                                        //保存到对象中
                                                        MemoryStream ms = new MemoryStream();
                                                        image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                                        byte[] imgByte = ms.ToArray();
                                                        duty.leaderImg = imgByte;
                                                    }
                                                }

                                                break;
                                        }
                                    }
                                }
                                catch (Exception e0)
                                {
                                    MessageBox.Show(e0.Message);
                                    throw new Exception(e0.Message);
                                }
                            }
                            //addDutyList.Add(duty);

                            int id = dutyBll.GetMaxId();
                            duty.ID = id;
                            try
                            {
                                dutyBll.add(duty, duty.leaderImg);
                            }
                            catch (Exception addE) {
                                MessageBox.Show(addE.Message);
                            }
                           
                        }
                        excel.Quit();
Global site tag (gtag.js) - Google Analytics