您的位置:首页 > 编程语言 > ASP

Aspose.Cells.dll的运用

2015-08-07 11:46 645 查看
读取表格:

  /// <summary>

/// 读取Excel表名

/// </summary>

/// <param name="Path">Excel路径</param>

/// <returns>表名数据集合</returns>

public static DataTable GetExcelTableName(string Path)

{

DataTable _Table = new DataTable();

try

{

if (System.IO.File.Exists(Path))

{

OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.ace.oledb.12.0;Extended Properties=\"Excel 12.0\";Data Source=" + Path);

_ExcelConn.Open();

_Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

_ExcelConn.Close();

}

return _Table;

}

catch

{

return _Table;

}

}

web方式的导出:

string name = DateTime.Now.ToString("yyyyMMddHHmmss") + Session["uid"].ToString();//表单的名称

Workbook wb = new Workbook();

wb.Worksheets.Clear();

wb.Worksheets.Add("海关申报报表 - " + datetxt.Value);//New Worksheet1是Worksheet的name即 表明

Worksheet ws = wb.Worksheets[0];

//1-12 列合并 写标题

ws.Cells.Merge(0, 0, 1, 11);

ws.Cells[0, 0].PutValue("海关申请报表 - " + DateTime.Parse(datetxt.Value).ToLongDateString());

//创建列名

ws.Cells[1, 0].PutValue("单号");

ws.Cells[1, 1].PutValue("袋号");

ws.Cells[1, 2].PutValue("寄件人");

ws.Cells[1, 3].PutValue("寄件人电话");

ws.Cells[1, 4].PutValue("寄件人地址");

ws.Cells[1, 5].PutValue("收件人");

ws.Cells[1, 6].PutValue("收件人电话");

ws.Cells[1, 7].PutValue("收件人地址");

ws.Cells[1, 8].PutValue("品名");

ws.Cells[1, 9].PutValue("重量");

ws.Cells[1, 10].PutValue("航空号");

//循环表读取数据,并填充

for (int i = 0; i < dt.Rows.Count; i++)

{

ws.Cells[i + 2, 0].PutValue(dt.Rows[i]["TrackingNo"]);

ws.Cells[i + 2, 1].PutValue(dt.Rows[i]["Description"]);

ws.Cells[i + 2, 2].PutValue(dt.Rows[i]["Sender"]);

ws.Cells[i + 2, 3].PutValue(dt.Rows[i]["FromPhoneNo"]);

ws.Cells[i + 2, 4].PutValue(dt.Rows[i]["FromAddress"]);

ws.Cells[i + 2, 5].PutValue(dt.Rows[i]["Receiver"]);

ws.Cells[i + 2, 6].PutValue(dt.Rows[i]["ToPhoneNo"]);

ws.Cells[i + 2, 7].PutValue(dt.Rows[i]["ToAddress"]);

string pname = "";

GetNameAndWeight(dt.Rows[i]["TrackingNo"].ToString(), ref pname);

ws.Cells[i + 2, 8].PutValue(pname);

ws.Cells[i + 2, 9].PutValue(dt.Rows[i]["ChargedWt"]);

ws.Cells[i + 2, 10].PutValue(dt.Rows[i]["AerialNo"]);

}

string path = ApplicationInfo.PhysicalApplicationPath + "Temp//" + name + ".xls";//保存的路径

#region 样式开始 在 wb.Save(path); 之前使用

Aspose.Cells.Style st = wb.DefaultStyle;

st.Borders.SetStyle(CellBorderType.Thin);

st.Borders.DiagonalStyle = CellBorderType.None;

StyleFlag stg = new StyleFlag();

stg.All = true;

ws.Cells.ApplyStyle(st, stg);

#endregion 样式结束

wb.Save(path);

Dc(path, name, "海关申报报表" + DateTime.Parse(datetxt.Value).ToLongDateString());

private void Dc(string path, string name, string cname)

{

FileInfo fi = new FileInfo(path);//excelFile为文件在服务器上的地址

HttpResponse contextResponse = HttpContext.Current.Response;

contextResponse.Clear();

contextResponse.Buffer = true;

contextResponse.Charset = "utf-8"; //设置了类型为中文防止乱码的出现

string filename = HttpUtility.UrlEncode(cname + ".xls");

contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)); //定义输出文件和文件名

contextResponse.AppendHeader("Content-Length", fi.Length.ToString());

contextResponse.ContentEncoding = Encoding.Default;

contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。

contextResponse.WriteFile(fi.FullName);

contextResponse.Flush();

contextResponse.End();

}

WF中直接去掉 Dc(path, name, "海关申报报表" + DateTime.Parse(datetxt.Value).ToLongDateString());方法即可。

如需要弹出客户自己选择路径,则自己加一个弹出控件即可。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: