永发信息网

C# Excel导出

答案:1  悬赏:20  手机版
解决时间 2021-11-15 14:14
  • 提问者网友:萌卜娃娃
  • 2021-11-15 10:03
C# Excel导出
最佳答案
  • 五星知识达人网友:夜风逐马
  • 2021-11-15 10:32
方法一:
  对于向我们这种有保安软件,所有文件加密,运行会很慢,但是可以执行编辑excel,可以添加vba,画线等等吧!
  原理就相当于,自己打开一个excel,编辑完,保存,关上。
  private void button2_Click(object sender, EventArgs e)
  {
  COMExcel.Application exApp = new COMExcel.Application();
  COMExcel.Workbook exBook = exApp.Workbooks.Add(COMExcel.XlWBATemplate.xlWBATWorksheet);
  COMExcel.Worksheet exSheet = (COMExcel.Worksheet)exBook.Worksheets[1];

  //exSheet.Activate();
  exSheet.Name = "Software Version";
  exSheet.Cells[1, 1] = "Line";
  exSheet.Cells[1, 2] = "Version";
  exSheet.Cells[1, 3] = "robot Version";
  
  SaveFileDialog saveFileDialog = new SaveFileDialog();
  saveFileDialog.Filter = "Excel files (*.xls) | *.xls";
  saveFileDialog.FilterIndex = 0;
  saveFileDialog.RestoreDirectory = true;
  saveFileDialog.CreatePrompt = true;
  saveFileDialog.Title = "Export Excel File To";
  try
  {
  if (saveFileDialog.ShowDialog() == DialogResult.OK)
  {
  if (saveFileDialog.FileName.Length > 0)
  {
  
  for (int i = 0; i < dgvVersion.Rows.Count-1; i++)
  {
  DataGridViewRow row = dgvVersion.Rows[i];
  for (int j = 0; j < row.Cells.Count; j++)
  {
  exApp.Cells[i + 2, j + 1] = (string)row.Cells[j].Value;
  }
  }
  exApp.Columns.AutoFit();
  exApp.ActiveWorkbook.SaveCopyAs(saveFileDialog.FileName);
  exApp.ActiveWorkbook.Saved = true;
  MessageBox.Show("Excel export successfully");
  }

  }
  }
  catch
  {  
  MessageBox.Show(" 保存文件失败 ", "警告");
  }
  exApp.Quit();
  }

  方法二:
  速度快,有保安软件也不怕
  原理,好像就是文件镜像,记不清了,自己查查吧!
  private void button_rf_ds_excel_Click(object sender, EventArgs e)
  {
  SaveFileDialog saveFileDialog = new SaveFileDialog();
  
  saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
  
  saveFileDialog.FilterIndex = 0;

  saveFileDialog.RestoreDirectory = true;

  saveFileDialog.CreatePrompt = true;

  saveFileDialog.Title = "SAVE AS Excel File";

  saveFileDialog.ShowDialog();

  string strName = saveFileDialog.FileName;

  if (strName != "")
  {
  //方法2
  Stream myStream;

  myStream = saveFileDialog.OpenFile();
  StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));

  string str = "";

  try
  {
  //写标题
  for (int i = 0; i < dataGridView_rf_ds_data.ColumnCount; i++)
  {
  if (i > 0)
  {
  str += "\t";
  }
  str += dataGridView_rf_ds_data.Columns[i].HeaderText;
  }
  sw.WriteLine(str);
  //写内容
  for (int j = 0; j < dataGridView_rf_ds_data.Rows.Count; j++)
  {
  string tempStr = "";
  for (int k = 0; k < dataGridView_rf_ds_data.Columns.Count; k++)
  {
  if (k > 0)
  {
  tempStr += "\t";
  }
  tempStr += dataGridView_rf_ds_data.Rows[j].Cells[k].Value.ToString();
  }
  sw.WriteLine(tempStr);
  }
  sw.Close();
  myStream.Close();
  MessageBox.Show("Export Successful!");
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.ToString());
  }
  finally
  {
  sw.Close();
  myStream.Close();
  }

  //MethodInvoker mi = new MethodInvoker(Exoprt);
  //mi.BeginInvoke(null, null);
  }
  }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯