天涯孤岸软件商城,.net电子商务商城源码,,以及其它源码, 欢迎访问:http://tyga.tygaweb.com/ (新)
使用javascript:Office JavaScript API library
https://appsforoffice.microsoft.com/lib/1.0/hosted/office.js
参考文章:http://msdn.microsoft.com/zh-cn/library/fp142185.aspx
NPOI 无需安装microsoft excel 相关文章:http://www.cnblogs.com/atao/category/209358.html
不使用微软提供程序,使用NPOI导出Excel.cs
/// <summary>
/// 以指定的文件名,数据创建Excel模板,并返回文件路径
/// </summary>
/// <param name="fileName"></param>
/// <param name="dt"></param>
/// <returns></returns>
public static string CreateDownloadTemplate(string fileName,DataTable dt)
{
string rootPath = HttpContext.Current.Server.MapPath(DOWNLOADTEMPLATE);
string fullPath = rootPath.TrimEnd('\\') + "\\" + fileName.TrimStart('\\');
string dir = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
DataColumnCollection cols = dt.Columns;
DataRowCollection rows = dt.Rows;
int colCount = cols.Count;
using (FileStream fs = new FileStream(fullPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
ISheet sheet = hssfworkbook.CreateSheet("Sheet1");
int n = -1;
foreach (DataColumn col in cols)
{
n++;
sheet.SetColumnWidth(n, 20 * 256);
if (n > 0)
{
ICell cell = sheet.GetRow(0).CreateCell(n);
cell.SetCellValue(col.Caption);
}
else
{
ICell cell = sheet.CreateRow(0).CreateCell(n);
cell.SetCellValue(col.Caption);
}
}
n = 0;
foreach (DataRow row in rows)
{
n++;
for (int i = 0; i < colCount; i++)
{
if (i > 0)
{
ICell cell = sheet.GetRow(n).CreateCell(i);
cell.SetCellValue(row[i].ToString());
}
else
{
ICell cell = sheet.CreateRow(n).CreateCell(i);
cell.SetCellValue(row[i].ToString());
}
}
}
hssfworkbook.Write(fs);
fs.Close();
}
string fileUrl = "~/" + fullPath.Replace(HttpContext.Current.Server.MapPath("~"), "").Replace("\\", "/").TrimStart('/');
fileUrl = VirtualPathUtility.MakeRelative("~/login.aspx", fileUrl);
return fileUrl;
}
导出