我是asp.net的新手,我想基于现有模板文件创建一个新的Excel电子表格,并使用存储过程中的数据填充它。 我尝试过使用EPPlus,但它没有用。
答案 0 :(得分:0)
(我确定这个问题已被问过几百次了。)
这是我提出的C#/ VB.Net解决方案。
完全免费下载,所有源代码都已提供。
它不使用现有模板,但它非常易于使用。只需一行代码,您只需传递一个DataSet
,DataTable
或List<>
,它就会使用OpenXML为您创建一个真实的Excel文件。
CreateExcelFile.CreateExcelDocument(myDataSet, myExcelPathFilename);
答案 1 :(得分:0)
你可以这样做:
public string Export(int entidadeID, int ccID, int paisID, bool mercApts)
{
string arquivo = "templateFile.xlsx";
//Template file is
string caminho = HttpContext.Current.Server.MapPath(@"~/site/premissas/");
//New file will be for download
string _caminho = HttpContext.Current.Server.MapPath(@"~/site/premissas/temp/");
string _arquivo = "newname.xlsx";
File.Copy(caminho + arquivo, _caminho + _arquivo);
File.SetAttributes(_caminho + _arquivo, FileAttributes.Normal);
string excelConnectString = "Provider= Microsoft.ACE.OLEDB.12.0;Data Source=" + _caminho + _arquivo + @";Extended Properties='Excel 12.0;HDR=No'";
//string excelConnectString = "Provider = Microsoft.Jet.OLEDB.4.0;" + @"Data Source = " + _caminho + _arquivo +@";" + "Extended Properties = 'Excel 8.0;HDR=No'";
OleDbConnection objConn = new OleDbConnection(excelConnectString);
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter objDatAdap = new OleDbDataAdapter();
DataSet ds = new DataSet();
#region Fill Sheet
string _nomePlanilha = "Plan1$";
cmd = new OleDbCommand("SELECT * FROM [" + _nomePlanilha + "]", objConn);
objDatAdap.SelectCommand = cmd;
objDatAdap.Fill(ds);
objConn.Open();
try
{
//F1, F2... are the columns in Excel, you can see it on ds DataSet filled above
cmd = new OleDbCommand("Update [" + _nomePlanilha + "] set F4 = '" + your values + @"' where F2 = 'Value to compare'", objConn);
cmd.ExecuteNonQuery();
}
catch { }
}