[WinForm]dataGridView导出到EXCEL

方法一:

 

[csharp][/csharp] view plaincopy

  1. SaveFileDialog dlg = new SaveFileDialog();
  2. dlg.Filter = “Execl files (*.xls)|*.xls”;
  3. dlg.FilterIndex = 0;
  4. dlg.RestoreDirectory = true;
  5. dlg.CreatePrompt = true;
  6. dlg.Title = “保存为Excel文件”;
  7. dlg.FileName = “不合格记录”;//保存的Excel名字
  8. if (dlg.ShowDialog() == DialogResult.OK)
  9. {
  10.     Stream myStream;
  11.     myStream = dlg.OpenFile();
  12.     StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
  13.     string columnTitle = “”;
  14.     try
  15.     {
  16.         //写入列标题
  17.         for (int i = 0; i < dgv.ColumnCount; i++)
  18.         {
  19.             if (i > 0)
  20.             {
  21.                 columnTitle += “\t”;
  22.             }
  23.             columnTitle += dgv.Columns[i].HeaderText;
  24.         }
  25.         sw.WriteLine(columnTitle);
  26.         //写入列内容
  27.         for (int j = 0; j < dgv.Rows.Count; j++)
  28.         {
  29.             string columnValue = “”;
  30.             for (int k = 0; k < dgv.Columns.Count; k++)
  31.             {
  32.                 if (k > 0)
  33.                 {
  34.                     columnValue += “\t”;
  35.                 }
  36.                 if (dgv.Rows[j].Cells[k].Value == null)
  37.                     columnValue += “”;
  38.                 else
  39.                     columnValue += dgv.Rows[j].Cells[k].Value.ToString().Trim();
  40.             }
  41.             sw.WriteLine(columnValue);
  42.         }
  43.         sw.Close();
  44.         myStream.Close();
  45.     }
  46.     catch (Exception e)
  47.     {
  48.         MessageBox.Show(e.ToString());
  49.     }
  50.     finally
  51.     {
  52.         sw.Close();
  53.         myStream.Close();
  54.     }
  55. }

 

 

方法二:包含图片

 

[csharp][/csharp] view plaincopy

  1. Microsoft.Office.Interop.Excel.Application Myexcel = new Microsoft.Office.Interop.Excel.Application();
  2.             if (Myexcel == null)
  3.             {
  4.                 return;
  5.             }
  6.             Microsoft.Office.Interop.Excel._Workbook xBk;
  7.             xBk = Myexcel.Application.Workbooks.Add(true);
  8.             Microsoft.Office.Interop.Excel._Worksheet xSt;
  9.             xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
  10.             //设置标题等
  11.             string Title = null;
  12.             Title = DateTime.Now.ToLongDateString() + “报价表”;
  13.             xSt.Name = Title;
  14.             //报表的格式设置
  15.             //xSt.Cells[1, 6] = Title;
  16.             // xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
  17.             // xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).Font.Bold = true;
  18.             //xSt.get_Range(Myexcel.Cells[1, 6], Myexcel.Cells[1, 6]).Font.Size = 20;
  19.             xSt.Cells[1, 1] = “品号”;
  20.             xSt.Cells[1, 2] = “品名”;
  21.             xSt.Cells[1, 3] = “客户品号”;
  22.             xSt.Cells[1, 4] = “图片”;
  23.             xSt.Cells[1, 5] = “客户编码”;
  24.             xSt.Cells[1, 6] = “客户名称”;
  25.             xSt.Cells[1, 7] = “数量”;
  26.             xSt.Cells[1, 8] = “币种”;
  27.             xSt.Cells[1, 9] = “汇率”;
  28.             xSt.Cells[1, 10] = “原币单价”;
  29.             xSt.Cells[1, 11] = “原币总价”;
  30.             xSt.Cells[1, 12] = “本币单价”;
  31.             xSt.Cells[1, 13] = “本币总价”;
  32.             xSt.Cells[1, 14] = “创建时间”;
  33.             //下面是用循环把datagridview中的内容写到excel
  34.             for (int rowIndex = 0; rowIndex < this.dgvQuotation.Rows.Count; rowIndex++)
  35.             {
  36.                 int colIndex = 0;
  37.                 for (colIndex = 1; colIndex <= dgvQuotation.ColumnCount; colIndex++)
  38.                 {
  39.                     String value = null;
  40.                     if (dgvQuotation.Rows[rowIndex].Cells[colIndex – 1].Value != null)
  41.                     {
  42.                         value = dgvQuotation.Rows[rowIndex].Cells[colIndex – 1].Value.ToString();
  43.                         if (dgvQuotation.Columns[colIndex – 1].Name == “图片”) //处理
  44.                         {
  45.                             Image img;
  46.                             //如果是二进制形式:
  47.                             //MemoryStream ms = new MemoryStream((byte[])dgvQuotation.Rows[rowIndex].Cells[colIndex – 1].Value);
  48.                             if (File.Exists(@”D:\产品图片\” + dgvQuotation.Rows[rowIndex].Cells[0].Value.ToString() + “.jpg”))
  49.                             {
  50.                                 //需要判断是否存在图片
  51.                                  img = Image.FromFile(@”D:\产品图片\” + dgvQuotation.Rows[rowIndex].Cells[0].Value.ToString() + “.jpg”);//双引号里是图片的路径
  52.                             }
  53.                             else
  54.                             {
  55.                                 //需要判断是否存在图片
  56.                                  img = Image.FromFile(@”D:\产品图片\LOGO.jpg”);//双引号里是图片的路径
  57.                             }
  58.                             //Image img = Image.FromStream(ms);
  59.                             //路径
  60.                            // Image img = GetImage(value);
  61.                             string tmpName = tmpPath + “\\temp” + rowIndex + “.bmp”;
  62.                             img.Save(tmpName);
  63.                             string cellAddr = (GetAddress(rowIndex + 4, colIndex));
  64.                             Microsoft.Office.Interop.Excel.Range range = (Microsoft.Office.Interop.Excel.Range)xSt.get_Range(cellAddr, Type.Missing);
  65.                             float left = float.Parse(range.Left.ToString());
  66.                             float top = float.Parse(range.Top.ToString());
  67.                             float width = float.Parse(range.Width.ToString());
  68.                             float height = float.Parse(range.Height.ToString());
  69.                             xSt.Shapes.AddPicture(tmpName, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, width, height);
  70.                             //xSt.Shapes.AddPicture(tmpName, MsoTriState.msoFalse, MsoTriState.msoTrue, left, top, 200, 200);
  71.                             continue;
  72.                         }
  73.                     }
  74.                     xSt.Cells[rowIndex + 4, colIndex] = value;
  75.                 }
  76.             }
  77.             //后台处理
  78.             //用户无法看到
  79.             Myexcel.Visible = false;
  80.             //允许打开对话框保存文件
  81.             Myexcel.DisplayAlerts = true;
  82.            // xBk.Close(true, “D:\\1.xls”, null);
  83.             xSt = null;
  84.             xBk = null;
  85.             Myexcel.Quit();
  86.             System.Runtime.InteropServices.Marshal.ReleaseComObject(Myexcel);
  87.             Myexcel = null;

标签