Aspose.cell.dll的使用,导excel表
using System;
using System.Web;
using EF;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Aspose.Cells;
//using Microsoft.Office.Interop.Excel;
//using System.Reflection; public class ToOverTimexls : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
int oname = 0, years = 0, month = 0;
if (context.Request["name"] != null)
{
oname = int.Parse(context.Request["name"]);
}
if (context.Request["years"] != null)
{
years = int.Parse(context.Request["years"]);
}
if (context.Request["month"] != null)
{
month = int.Parse(context.Request["month"]);
}
//oname = 1; years = 2016; month = 1;
using (WorkRecordEntities db = new WorkRecordEntities())
{
IList<OverTime> list = db.OverTime.Where(o => o.StaffID == oname && o.StartTime.Year == years && o.StartTime.Month == month).ToList();
var name = db.Staff.Where(o => o.StaffID == oname).FirstOrDefault().FullName;
//建立一个Excel进程 Application
// string SavaFilesPath = System.Configuration.ConfigurationManager.AppSettings["DownLoad"] + Guid.NewGuid() + ".xls";
string SavaFilesPath = context.Server.MapPath("~/Download") + "\\" + Guid.NewGuid() + ".xls";
// Application excelApplication = new Application(); // //默认值为 True。如果不想在宏运行时被无穷无尽的提示和警告消息所困扰,请将本属性设置为 False;这样每次出现需用户应答的消息时,Microsoft Excel
// // 将选择默认应答。
// //如果将该属性设置为 False,则在代码运行结束后,Micorosoft Excel 将该属性设置为 True,除非正运行交叉处理代码。
// //如果使用工作簿的 SaveAs 方法覆盖现有文件,“覆盖”警告默认为“No”,当 DisplayAlerts 属性值设置为 True 时,Excel 选择“Yes”。
// excelApplication.DisplayAlerts = false; // // 建立或打开一个 Workbook对象生成新Workbook
// Workbook workbook = excelApplication.Workbooks.Add(Missing.Value);
// //int x = 2;
// Worksheet lastWorksheet = (Worksheet)workbook.Worksheets.get_Item(workbook.Worksheets.Count);
// Worksheet newSheet = (Worksheet)workbook.Worksheets.Add(Type.Missing, lastWorksheet, Type.Missing, Type.Missing);
// //表头
// newSheet.Cells[4, 1] = "No.(序号)";
// newSheet.Cells[4, 2] = "Date(日期)";
// newSheet.Cells[4, 3] = "Mon.~Sun.";
// newSheet.Cells[4, 4] = "From(开始)";
// newSheet.Cells[4, 5] = "To(止)";
// newSheet.Cells[4, 6] = "OT Hrs.(时间)";
// newSheet.Cells[4, 7] = "Evnt(加班事由)";
// newSheet.Cells[4, 8] = "Approve(审批)";
// newSheet.Cells[3, 4] = "To(加班时间)";
// newSheet.Cells[1,4]="加班申请表";
// newSheet.Cells[2,1]="部门:后台";
//// newSheet.Cells[2, 2] = "后台";
// newSheet.Cells[2, 6] = "姓名:"+name;
// //newSheet.Cells[2, 7] = name;
// newSheet.get_Range("A1", "H1").Merge(newSheet.get_Range("A1", "H1").MergeCells);
// newSheet.get_Range("A2", "D2").Merge(newSheet.get_Range("A2", "D2").MergeCells);
// newSheet.get_Range("F2", "H2").Merge(newSheet.get_Range("F2", "H2").MergeCells);
// newSheet.get_Range("A3", "A4").Merge(newSheet.get_Range("A3", "A4").MergeCells);
// newSheet.get_Range("B3", "B4").Merge(newSheet.get_Range("B3", "B4").MergeCells);
// newSheet.get_Range("C3", "C4").Merge(newSheet.get_Range("C3", "C4").MergeCells);
// newSheet.get_Range("D3", "F3").Merge(newSheet.get_Range("D3", "F3").MergeCells); // newSheet.get_Range("G3", "G4").Merge(newSheet.get_Range("G3", "G4").MergeCells);
// newSheet.get_Range("H3", "H4").Merge(newSheet.get_Range("H3", "H4").MergeCells);
// newSheet.get_Range("A3", "H3").Interior.ColorIndex = 15;
// newSheet.get_Range("A4", "H4").Interior.ColorIndex = 15;
// Range range1 = newSheet.get_Range("A1", "H1");
// range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;
// range1.WrapText = true;
// Range range = newSheet.get_Range("A3", "H4"); // range.HorizontalAlignment = XlHAlign.xlHAlignCenter;
// range.Font.Size = 10;
// range.Borders.LineStyle = 1;
// //设置边框
//// range.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlMedium, XlColorIndex.xlColorIndexAutomatic, System.Drawing.Color.Black.ToArgb());
// range.Borders.get_Item(XlBordersIndex.xlEdgeBottom).Weight = XlBorderWeight.xlMedium;
// range.WrapText = true; //// var x = 3;
// var x = 5;
// for (var i = 0; i < list.Count; i++)
// {
// newSheet.Cells[x + i, 1] = i + 1;
// newSheet.Cells[x + i, 2] = list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日";
// newSheet.Cells[x + i, 3] = GetWeekCHA((list[i].StartTime.DayOfWeek).ToString());
// newSheet.Cells[x + i, 4] = list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss");
// newSheet.Cells[x + i, 5] = list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss");
// int ts = (list[i].EndTime - list[i].StartTime).Hours;
// newSheet.Cells[x + i, 6] = ts;
// newSheet.Cells[x + i, 7] = list[i].Description;
// newSheet.Cells[x + i, 8] = "";
// } // newSheet.Cells.Columns.AutoFit();
// //删除原来的空Sheet
// ((Worksheet)workbook.Worksheets.get_Item(1)).Delete();
// ((Worksheet)workbook.Worksheets.get_Item(1)).Delete();
// ((Worksheet)workbook.Worksheets.get_Item(1)).Delete();
// //设置默认选中是第一个Sheet 类似于Select();
// ((Worksheet)workbook.Worksheets.get_Item(1)).Activate();
// try
// {
// workbook.Close(true, SavaFilesPath, Missing.Value);
// }
// catch (Exception e)
// {
// throw e;
// }
// UploadExcel(SavaFilesPath, true);
// excelApplication.Quit();
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
worksheet.PageSetup.Orientation = PageOrientationType.Landscape;//横向打印
worksheet.PageSetup.Zoom = 100;//以100%的缩放模式打开
worksheet.PageSetup.PaperSize = PaperSizeType.PaperA4;
Range range; Cell cell;
range = worksheet.Cells.CreateRange(0, 0, 1, 8);
range.Merge();
range.RowHeight = 20;
range.ColumnWidth = 15;
cell = range[0, 0];
cell.PutValue("加班申请表");
cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(1, 0, 1, 2);
range.Merge();
range.RowHeight = 15;
cell = range[0, 0];
cell.PutValue("部门:后台"); range = worksheet.Cells.CreateRange(1, 4, 1, 2);
range.Merge();
range.RowHeight = 15;
cell = range[0, 0];
cell.PutValue("姓名:" + name); //range = worksheet.Cells.CreateRange(1, 5, 1, 1);
//range.Merge();
//range.RowHeight = 15;
//cell = range[0, 0];
//cell.PutValue("方亭"); range = worksheet.Cells.CreateRange(2, 0, 2, 1);
range.Merge();
cell = range[0, 0];
cell.PutValue("No.(序号)");
cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 1, 2, 1);
range.Merge();
cell = range[0, 0];
cell.PutValue("Date(日期)"); range = worksheet.Cells.CreateRange(2, 2, 2, 1);
range.Merge();
cell = range[0, 0];
cell.PutValue("Mon.~Sun.");
cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 3, 1, 3);
range.Merge();
range.ColumnWidth = 20;
cell = range[0, 0];
cell.PutValue("To(加班时间)");
cell.SetStyle(styleTitle); cell = worksheet.Cells[3, 3];
cell.PutValue("From(开始)");
cell.SetStyle(styleTitle);
cell = worksheet.Cells[3, 4];
cell.PutValue("To(止)");
cell.SetStyle(styleTitle);
cell = worksheet.Cells[3, 5];
cell.PutValue("OT Hrs.(时间)");
cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 6, 2, 1);
range.Merge();
cell = range[0, 0];
cell.PutValue("Evnt(加班事由)");
cell.SetStyle(styleTitle); range = worksheet.Cells.CreateRange(2, 7, 2, 1);
range.Merge();
cell = range[0, 0];
cell.PutValue("Approve(审批)");
cell.SetStyle(styleTitle);
for (var i = 0; i < list.Count; i++)
{
//newSheet.Cells[x + i, 1] = i + 1;
//newSheet.Cells[x + i, 2] = list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日";
//newSheet.Cells[x + i, 3] = GetWeekCHA((list[i].StartTime.DayOfWeek).ToString());
//newSheet.Cells[x + i, 4] = list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss");
//newSheet.Cells[x + i, 5] = list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss");
//int ts = (list[i].EndTime - list[i].StartTime).Hours;
//newSheet.Cells[x + i, 6] = ts;
//newSheet.Cells[x + i, 7] = list[i].Description;
//newSheet.Cells[x + i, 8] = "";
cell = worksheet.Cells[4 + i, 0];
cell.PutValue(i + 1);
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 1];
cell.PutValue(list[i].StartTime.Month + "月" + list[i].StartTime.Day + "日");
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 2];
cell.PutValue(GetWeekCHA((list[i].StartTime.DayOfWeek).ToString()));
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 3];
cell.PutValue(list[i].StartTime.ToString("yyyy/MM/dd HH:mm:ss"));
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 4];
cell.PutValue(list[i].EndTime.ToString("yyyy/MM/dd HH:mm:ss"));
cell.SetStyle(styleTitle);
int ts = (list[i].EndTime - list[i].StartTime).Hours;
cell = worksheet.Cells[4 + i, 5];
cell.PutValue(ts);
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 6];
cell.PutValue(list[i].Description);
cell.SetStyle(styleTitle);
cell = worksheet.Cells[4 + i, 7];
cell.PutValue("");
cell.SetStyle(styleTitle);
}
workbook.Save(SavaFilesPath);
UploadExcel(SavaFilesPath, true);
}
}
/// <summary>
/// 返回星期中文名
/// </summary>
/// <param name="WeekENG">星期英文名</param>
/// <returns></returns>
public string GetWeekCHA(string WeekENG)
{
string return_value = "";
switch (WeekENG)
{
case "Monday":
return_value = "星期一";
return return_value;
case "Tuesday":
return_value = "星期二";
return return_value;
case "Wednesday":
return_value = "星期三";
return return_value;
case "Thursday":
return_value = "星期四";
return return_value;
case "Friday":
return_value = "星期五";
return return_value;
case "Saturday":
return_value = "星期六";
return return_value;
case "Sunday":
return_value = "星期日";
return return_value;
}
return return_value;
}
/// <summary>
/// 提供下载
/// </summary>
/// <param name="path"></param>
/// <param name="page"></param>
/// <param name="isDelete"></param>
private static void UploadExcel(string path, bool isDelete)
{
FileInfo file = new FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
HttpContext.Current.Response.WriteFile(file.FullName);
HttpContext.Current.Response.Flush();
if (isDelete)
{
File.Delete(path);
}
// 停止页面的执行
HttpContext.Current.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
} } js
//下载Excel
window.location.href = "/ashx/ToExcel.ashx?project=" + probject + "&years=" + years + "&month=" + month;
Aspose.cell.dll的使用,导excel表的更多相关文章
- Aspose.Cells.dll引用导入导出Excel
Aspose.Cells 导入导出EXCEL 文章出处:http://hi.baidu.com/leilongbing/item/c11467e1819e5417595dd8c1 修改样式 ...
- 使用Aspose.Cell.dll导出Excel总结
这两天项目上用Aspose导出Excel来着.开始感觉挺简单的,但是实际操作起来还是挺复杂的,调试占的时间很长.主要是动态生成列.合并单元格.调样式占了很长时间,还是总结一下吧. 基础操作: //EX ...
- 使用aspose.cell动态导出多表头 EXCEL
效果图: 前台调用: using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- 利用aspose.cell把数据导出到excel
/// <summary> /// 导出数据到本地 /// </summary> /// <param name="dt">要导出的数据< ...
- 传参导出Excel表乱码问题解决方法
业务场景 先描述一下业务场景,要实现的功能是通过搜索框填写参数,然后点击按钮搜索数据,将搜索框的查询参数获取,附加在链接后面,调导Excel表接口,然后实现导出Excel功能.其实做导Excel表功能 ...
- C# WinForm 导出导入Excel/Doc 完整实例教程[使用Aspose.Cells.dll]
[csharp] view plain copy 1.添加引用: Aspose.Cells.dll(我们就叫工具包吧,可以从网上下载.关于它的操作我在“Aspose.Cells操作说明 中文版 下载 ...
- 使用aspose.cell导出excel需要注意什么?
1.如果导出的数据源是汇总出来的,最好方法是将数据源放到缓存里面,当基本数据源变化的时候,在改变数据2.使用模板导出EXCEL,这样很多样式可以在模板文件里面直接设置,例如:默认打开页签,让列头固定3 ...
- 利用Aspose.Cell控件导入Excel非强类型的数据
导入Excel的操作是非常常见的操作,可以使用Aspose.Cell.APOI.MyXls.OLEDB.Excel VBA等操作Excel文件,从而实现数据的导入,在导入数据的时候,如果是强类型的数据 ...
- 使用Aspose.Cell控件实现Excel高难度报表的生成(三)
在之前几篇文章中,介绍了关于Apsose.cell这个强大的Excel操作控件的使用,相关文章如下: 使用Aspose.Cell控件实现Excel高难度报表的生成(一) 使用Aspose.Cell控件 ...
随机推荐
- 使用Nlog记录日志到数据库
Nlog是一个很不错的.NET日志记录组件,它可以将日志输出到控件台,保存到文本,也可以很方便的记录到数据库中. 可以在这里下载Nlog:http://nlog-project.org/ 这里分享一下 ...
- C++学习16 继承时的名字遮蔽
如果派生类中的成员变量和基类中的成员变量重名,那么就会遮蔽从基类继承过来的成员变量.所谓遮蔽,就是使用新增的成员变量,而不使用继承来的. 成员函数也一样,如果函数名和参数签名都相同,就会造成遮蔽.如果 ...
- [Flex] ButtonBar系列——垂直布局
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- 使用jQuery解析xml时command节点解析失败
jQuery版本1.8.3 待解析的xml为: <message><user><command>Login</command></message& ...
- Isilon OneFS Simulator Install Guide
Isilon build for storage data Use VMware converter to convert node1 to ESX(参考silon_OneFS_Simulator_I ...
- ReverseBits
eclipse没问题,leetcode 1不能通过,超出int最大值了,但是怎么转无符号? /*Write a function that takes an unsigned integer and ...
- Android——Gallery 图片拖动效果
1.XML文件 <?xml version="1.0" encoding="utf-8"?><Gallery xmlns:android=&q ...
- POJ 3207 Ikki's Story IV - Panda's Trick
Ikki's Story IV - Panda's Trick Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7296 ...
- CCombobox AddString出错
1.CComboBox放在对话框构造函数中, 这时CCombobox对象还没建立, 所以出错, 应该放在OnInitDialog()中初始化. 2. 使用的vs2005以上的IDE, 拖拉控件, 添加 ...
- python-appium手机自动化测试(仅需安装包)前期准备(pydev-eclipse编辑器)
1.jdk安装与环境变量配置教程http://jingyan.baidu.com/article/6dad5075d1dc40a123e36ea3.html 我本机安装的是1.6.043 2.sdk下 ...