string file =
Server.MapPath("~/Excel/ZWxxtj.xls");
DataSet ds = new
DataSet();
DataTable dtout = new
DataTable();
dtout.Columns.Add("单位",
typeof(string));
dtout.Columns.Add("数量",
typeof(string));
dtout.Columns.Add("分数",
typeof(string));
dtout.Columns.Add("看看", typeof(string));
List<DataTable> lst =
null;
for
(int i = 0; i < 3;
i++)
{
lst = new List<DataTable>();
DataRow dr =
dtout.NewRow();
dr["单位"] = "AAAA" +
i;
dr["数量"] = "BBBB" +
i;
dr["分数"] = "CCCC" +
i;
dr["看看"] = "DDDD" +
i;
dtout.Rows.Add(dr);
}
ds.Tables.Add(dtout);
lst.Add(ds.Tables[0]);
ExportExcelModel(this.Response, lst, file, 4);//从第四行开始填充数据
public void ExportExcelModel(HttpResponse res,
List<System.Data.DataTable> Datas, string ExcelTemplatePath, int
FirstRow)
{
//Excel的路径
是放excel模板的路径
WorkbookDesigner designer = new
WorkbookDesigner();
designer.Open(ExcelTemplatePath);
Worksheet
sheet =
designer.Workbook.Worksheets[0];
sheet.Cells.ImportDataTable(Datas[0], false, FirstRow, 0, true);
var c11 = sheet.Cells[0, 0];//第一行 第一列
c11.PutValue("我是标题,大家新年快乐。。。");
SaveOptions s = new
XlsSaveOptions(SaveFormat.Excel97To2003);
string str = "";
str = HttpUtility.UrlEncode("测试.xls", Encoding.UTF8).ToString();
designer.Workbook.Save(res, str, ContentDisposition.Attachment, s);
}
原文:http://www.cnblogs.com/cxlings/p/3550023.html