public class Infos
{
public int i1 { get; set; }
public int i2 { get; set; }
public double i3 { get; set; }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.DDF;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
List<Infos> list=new List<Infos>() {
new Infos { i1=99,i2=10,i3=0.48 },
new Infos { i1=88,i2=18,i3=0.68 },
new Infos { i1=77,i2=20,i3=0.28 },
new Infos { i1=95,i2=22,i3=0.94 },
new Infos { i1=92,i2=70,i3=0.50 },
};
string filePath = string.Empty;
HSSFWorkbook workbook = null;
HSSFSheet sheet1 = null;
HSSFRow row = null;
int nowRowNum = 1;
if (list.Count < 1)
{
Console.WriteLine("没有数据");
return;
}
string excelTempPath = System.Environment.CurrentDirectory + @"\temp\Excels.xls";
using (FileStream fs = new FileStream(excelTempPath, FileMode.Open, FileAccess.Read))
{
workbook = new HSSFWorkbook(fs);
}
sheet1 = workbook.GetSheetAt(0);
for (int i = 0; i < list.Count; i++)
{
row = sheet1.GetRow(nowRowNum);
row.GetCell(1).SetCellValue(list[i].i1);
row.GetCell(2).SetCellValue(list[i].i2);
row.GetCell(3).SetCellValue(list[i].i3);
nowRowNum++;
}
Console.WriteLine("保存?请输入路径,退出输入exit");
string str = Console.ReadLine();
if (str=="exit")
{
return;
}
else
{
FileStream file = new FileStream(str + @"\Exceldemos.xls", FileMode.Create);
workbook.Write(file);
file.Close();
}
}
}
}