首页 > 代码库 > C#写入excel和txt

C#写入excel和txt

 

写入txt:           

             string path = Server.MapPath("~/") + "\\Content\\删除记录.txt";           

             string w = DateTime.Now.ToString() + "----" + zfry.userid + "----" + zfry.username;
            w += "----" + zfry.ssqmc + "----" + zfry.bmmc + "----" + qymc + "----" + qydz;
            StreamWriter sw =System.IO.File.AppendText(path);
            sw.WriteLine(w);
            sw.Close();

写入excel:

            string path = Server.MapPath("~/") + "\\Content\\删除记录.txt";
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileStream(path, FileMode.Open));
            HSSFSheet sheet = (HSSFSheet)hssfWorkbook.GetSheetAt(0);
            int i = sheet.LastRowNum;
            HSSFRow row = (HSSFRow)sheet.CreateRow(i+1);
            row.CreateCell(0).SetCellValue(DateTime.Now.ToString());
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            hssfWorkbook.Write(fs);

C#写入excel和txt