需要引用的DLL文件:Aspose.Cells.dll,Aspose.Pdf.dll,Aspose.Words.dll,O2S.Components.PDFRender4NET.dll,Util.dll
管理系统,做导出功能的,可以用到
代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
/// <summary>
/// ImportExcel 的摘要说明
/// </summary>
public class ImportExcel
{
public ImportExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static string Import(string StrWhere)
{
try
{
DataSet ds2 = huahaocms.com.DbHelper.DbHelperSQL.Query(StrWhere);
if (ds2.Tables[0].Rows.Count == 0)
{
return "0";
}
Aspose.Cells.Workbook wkBook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet sheet = wkBook.Worksheets["Sheet1"];
sheet.Cells.ImportDataTable(ds2.Tables[0], true, 0, 0, ds2.Tables[0].Rows.Count, ds2.Tables[0].Columns.Count, false, "yyyy-MM-dd", false);
string _fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + "." + "xls"; //随机文件名
//按日期归类保存
string _datePath = DateTime.Now.ToString("yyyyMMdd") + "/";
string filePath = "/upload/file/" + _datePath;
//获得要保存的文件路径
string serverFileName = filePath + _fileName;
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(filePath);
//检查是否有该路径没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//将要保存的完整文件名
string toFile = toFileFullPath + _fileName;
//导出保存
wkBook.Save(toFile, Aspose.Cells.FileFormatType.Excel97To2003);
//释放对象
sheet = null;
wkBook = null;
return serverFileName;
}
catch (Exception ex)
{
return "1";
}
}
}