日期:2014-05-18  浏览次数:20748 次

导出excel问题
下面这个函数实现导出excel,但是不太清楚怎么分页,数据有26万条,一个sheet只可以放65536条,
所以我现在用它只可以导出一页,速度倒是可以,另外我用excel.Application这个方法也做了一个分页的例子,但是速度超级慢,不是一般的慢,那个函数就不发了,求高手给一个流方式分sheet导出exel的用法,谢谢大家!
public void ClearData(DataTable dtsouce, ProgressBar progreesBar, SaveFileDialog saveFileDialog) 
  {
  if (dtsouce.Rows.Count == 0)
  {
  MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  return;
  }
  else
  {
  saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
  saveFileDialog.FilterIndex = 0;
  saveFileDialog.RestoreDirectory = true;
  saveFileDialog.CreatePrompt = true;
  saveFileDialog.Title = "导出文件保存路径";
  saveFileDialog.ShowDialog();
  progreesBar.Visible = true;
  Stream myStream;
  myStream = saveFileDialog.OpenFile();
  //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
  StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
  string str = "";
  try
  {

  str = "编号" + "\t" + "产品名" + "\t" + "过程" + "\t" + "员工" + "\t" + "日期" + "\t" + "时间" + "\t" + "皮重厂商" + "\t" + "净重" + "\t" + "皮重";

  sw.WriteLine(str);
  //写内容
  for (int j = 0; j < dtsouce.Rows.Count - 1; j++)
  {
  string tempStr = "";
  tempStr = dtsouce.Rows[j]["F_SID"] + "\t" + dtsouce.Rows[j]["F_NAME"] + "\t" + dtsouce.Rows[j]["F_PROC"] + "\t" + dtsouce.Rows[j]["F_USER"] + "\t" + dtsouce.Rows[j]["F_DATE"] + "\t" + dtsouce.Rows[j]["F_TIME"] + "\t" + dtsouce.Rows[j]["F_NO"] + "\t" + dtsouce.Rows[j]["F_NW"] + "\t" + dtsouce.Rows[j]["F_TARE"];

  sw.WriteLine(tempStr);
  progreesBar.Value += 100 / dtsouce.Rows.Count;
  }
  sw.Close();
  myStream.Close();
  progreesBar.Value = 100;
  MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
  progreesBar.Value = 0;
  progreesBar.Visible = false;
  }
  catch (Exception e)
  {
  MessageBox.Show(e.Message, "友情提示", MessageBoxButtons.OK);
  }
  finally
  {
  sw.Close();
  myStream.Close();
  }
  }
  }

------解决方案--------------------
用NPOI,不用安装Office就可以写Excel,速度还不错
------解决方案--------------------
写个自动新增sheet,在存数据