private void btnOutput_Click(object sender, EventArgs e) { List<MODEL.Classes> list = cm.GetClassInfo(false); //获取对象数据集合 HSSFWorkbook workbook=new HSSFWorkbook (); //新建Excel工作表 HSSFSheet sheet=workbook.CreateSheet("classes"); //在工作文档中新建页 for(int i=0;i<list.Count;i++) //遍历lis对象数据集合 { HSSFRow row=sheet.CreateRow(i); //在页中创建行 HSSFCell cell1=row.CreateCell(0); //在行中创建列 cell1.SetCellValue(list[i].CID); //给列字段赋值 HSSFCell cell2=row.CreateCell(1); cell2.SetCellValue(list[i].CName); HSSFCell cell3=row.CreateCell(2); cell3.SetCellValue(list[i].CCount); HSSFCell cell4=row.CreateCell(3); cell4.SetCellValue(list[i].CImg); HSSFCell cell5=row.CreateCell(4); cell5.SetCellValue(list[i].CIsDel); HSSFCell cell6=row.CreateCell(5); cell6.SetCellValue(list[i].CAddTime); HSSFDataFormat date = workbook.CreateDataFormat(); //为工作表创建日期格式 HSSFCellStyle style = workbook.CreateCellStyle(); //为工作表创建单元格格式 style.DataFormat = date.GetFormat("yyyy-mm-dd hh-mm-ss"); //将日期格式赋值到单元格格式中 cell6.CellStyle = style; //设置具体单元格格式为已定义的日期格式 } using(FileStream fs=new FileStream (txtOutPut.Text.Trim(),FileMode.Create)) //获取路径并创建对应文件 { workbook.Write(fs); //写入对应文件 MessageBox.Show("写入成功"); } }
private void btnInput_Click(object sender, EventArgs e) { List<MODEL.Classes> list = new List<MODEL.Classes>(); //实例化集合对象接受数据 using(FileStream fs=new FileStream (txtInput.Text.Trim(),FileMode.Open)) //打开对应文件 { HSSFWorkbook workbook = new HSSFWorkbook(fs); //读取工作表内容 HSSFSheet sheet = workbook.GetSheet("classes"); //读取页内容 for(int i=sheet.FirstRowNum;i<sheet.LastRowNum;i++) //遍历行 { MODEL.Classes temp = new MODEL.Classes(); //实例化对象接受数据 HSSFRow row = sheet.GetRow(i); //读取行内容 temp.CID = (int)row.GetCell(0).NumericCellValue; temp.CName = row.GetCell(1).StringCellValue; temp.CCount =(int) row.GetCell(2).NumericCellValue; temp.CImg = row.GetCell(3).StringCellValue; temp.CIsDel = row.GetCell(4).BooleanCellValue; temp.CAddTime = row.GetCell(5).DateCellValue; list.Add(temp); } } this.dgvClassList.DataSource = list; }
时间: 2024-10-20 07:46:33