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());方法即可。
如需要弹出客户自己选择路径,则自己加一个弹出控件即可。
/// <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());方法即可。
如需要弹出客户自己选择路径,则自己加一个弹出控件即可。
相关文章推荐
- ASP.Net在web.config中设置上传文件的大小方法 (转)
- [CasperJS] 模拟登陆人人网
- ASPxPopupControl 弹窗不显示上一次内容的方法
- ASP.NET.4 高级程序第4版 第3章Web窗体
- ASP.Net服务端基本控件介绍
- CasperJS 介绍以及Windows上安装介绍
- ASP.NET MVC — 第 4 天
- 在AspNet.WebApi中使用Razor
- ASP.NET CSS 小结
- 基于SSH2框架AspectJ的登录登出日志实现
- jasper
- 全新的membership框架Asp.net Identity(2)——绕不过的Claims
- Winserver2008R2 .netframework4.5 asp.netmvc 访问出现的是文件列表。
- ASP.NET中日历控件和JS版日历控件的使用方法(第5节)
- ABP(现代ASP.NET样板开发框架)系列之20、ABP展现层——动态生成WebApi
- ASP.NET中验证控件的使用方法
- ASP.NET中验证控件的使用方法
- ASP.NET中日历控件和JS版日历控件的使用方法
- metasploit - browser_autopwn2
- 【asp】Jmail组件发邮件