1.表结构
2.读取数据表,循环绑定
public static DataTable GetData()
{
var strSql = "SELECT * FROM CICUser";
var strConn = ConfigurationManager.ConnectionStrings["linq1"].ConnectionString;
using (var conn = new SqlConnection(strConn))
{
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(strSql, conn);
adapter.Fill(dt);
return dt;
}
}
var data = GetData();
var row0 = sheet1.CreateRow(0);
for (int i = 0; i < data.Rows.Count; i++)
{
var row1 = sheet1.CreateRow(i+1);
for (int j = 0; j < data.Columns.Count; j++)
{
var cell0 = row0.CreateCell(j);
cell0.SetCellValue(data.Columns[j].ColumnName);
var cell1 = row1.CreateCell(j);
cell1.SetCellValue(data.Rows[i][j].ToString());
}
}
效果: