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

dictionary 只能遍历最后一个
OneAnnal()//是加载一条记录函数

  private void butAdd_Click(object sender, EventArgs e)
  {//添加按钮
  if (estimate())
  {
  OneAnnal();
  liquidation();//清空
  singers.Add(singer.Stunumber,singer);//把每条记录加载到 singers
  }
  }

  private void butSave_Click(object sender, EventArgs e)
  {//保存按钮,保存到Excel
  string fileName = Application.StartupPath + "\\data\\"+classNumber+".xls";//获取文件路径
  //加载excel
  MSExcel.Application excelApp;
  MSExcel.Workbook excelDoc;
  excelApp = new MSExcel.ApplicationClass();
  Object Nothing = Missing.Value;
  excelDoc = excelApp.Workbooks.Add(Nothing);
  MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];

  int i = 3;//从第三行开始写入
  foreach (Basis oneL in singers.Values)
  {//遍历dictionary
  ws.Cells[i, 1] = oneL.Stunumber;
  ws.Columns.AutoFit();//自动调整宽度
  ws.Cells[i, 2] = oneL.Stuname;
  ws.Columns.AutoFit();
  ws.Cells[i, 3] = oneL.Idcard;
  ws.Columns.AutoFit();
  ws.Cells[i, 4] = oneL.Sex;
  ws.Columns.AutoFit();
  ws.Cells[i, 5] = oneL.Birthrate;
  ws.Columns.AutoFit();
  ws.Cells[i, 6] = oneL.Nation;
  ws.Columns.AutoFit();
  ws.Cells[i, 7] = oneL.Handsetnumber;
  ws.Columns.AutoFit();
  ws.Cells[i, 8] = oneL.QQ;
  ws.Columns.AutoFit();
  ws.Cells[i, 9] = oneL.Bedchamber;
  ws.Columns.AutoFit();
  ws.Cells[i, 10] = oneL.Houseaddress;
  ws.Columns.AutoFit();
  ws.Cells[i, 11] = i.ToString();
  i++;//行数增加
  }

现在写到excel都是最后一条记录

------解决方案--------------------
探讨
引用:
butAdd_Click 方法里加上下面红字那行
private void butAdd_Click(object sender, EventArgs e)
{//添加按钮
if (estimate())
{
singer = new Basis();
OneAnnal();
liquidation();//清空
singers.Add(singer.S……