C# 实现Excel导出 加载模板 导入数据到模板
2010-04-23 14:58
1026 查看
前些天公司做了个项目,遇到要导出数据到Excel,并要求有格式,不用客户在去排版。本来用水晶报表之类的插件,做这个比较好,公司不愿掏钱,只好自己写程序去实现。 关于格式的控制有两种方案:一、填充数据时或田中完数据后在程序中控制格式;二、写好模板,格式固定好,只往里填数据就好了。对于第一种方案,在我前一篇日志里有介绍,下面介绍一下第二种方案。
using Excel;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Configuration;
public partial class UcTaskList : Tobto.DJ.DJWebUI.UIClass.DJUcListPageBase
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private ResultPacket ToExcel()
{
ResultPacket rp = new ResultPacket();
string excelTemplateDPath;
//EXCEL模板默认服务器物理存放路径
string tempEFilePath = " //TempFiles";
//EXCEL临时文件保存服务器物理存放路径
string tempEFileXPath = "/TempFiles";
//EXCEL临时文件保存服务器虚拟存放路径
Excel.Application app = null;
//EXCEL对象
Workbooks workbooks;
//工作簿集合
_Workbook workbook;
//当前工作簿
Sheets sheets;
//SHEET页集合
_Worksheet worksheet;
//当前SHEET页 #region 读取配置文件中路线模板路径及名称并验证是否存在
//获取配置文件中路线模板路径及名称
excelTemplateDPath = Server.MapPath(ConfigurationManager.AppSettings["ExcelTemplateDPath"]);
tempEFileXPath = ConfigurationManager.AppSettings["ExcelTemplateDPath"] + tempEFileXPath;
tempEFilePath = excelTemplateDPath + tempEFilePath; excelTemplateDPath += "//TaskTemplet.xls";
//验证EXCEL模板文件是否存在 if (!File.Exists(excelTemplateDPath)) { rp.IsError = true; rp.Message = "配置文件中指定的模板文件不存在"; return rp; }
//验证EXCEL临时文件夹是否存在
if (!File.Exists(tempEFilePath))
{ Directory.CreateDirectory(tempEFilePath); }
#endregion
#region
//从配置文件中取列名为填充数据做准备 ArrayList list = new ArrayList(); if (ConditionArr != null && ConditionArr.Count > 0) { list = TaskManageBrl.Find(ConditionArr[0], ConditionArr[1], ConditionArr[2], ConditionArr[3], bool.Parse(ConditionArr[4]), ConditionArr[5], ConditionArr[6], ConditionArr[7], 0, 10000, true, CurrentLoginUserInfo); } #endregion try { #region 启动excel进程并加载模板
//启动EXCEL进程
app = new Excel.Application();
if (app == null)
{
rp.IsError = true; rp.Message = "Excel进程启动出错,请确认是否引用EXCEL组件";
return rp;
}
app.Visible = false;
app.UserControl = true;
app.DisplayAlerts = false;
//加载读取模板
workbooks = app.Workbooks;
workbook = workbooks.Add(excelTemplateDPath);
sheets = workbook.Worksheets; worksheet = (_Worksheet)sheets.get_Item(1);
#endregion
#region
//填充数据
for (int i = 3; i < list.Count; i++)
{
worksheet.Cells[i, 1] = "Emon任务"; worksheet.Cells[i, 2] = "343";
worksheet.Cells[i, 3] = "233";
}
#endregion
#region 保存临时文件 worksheet.Columns.AutoFit();
//设置自动列宽
try {
string tempFileName = "TaskList" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
tempEFilePath += "//" + tempFileName; tempEFileXPath += "/" + tempFileName;
worksheet.SaveAs(tempEFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
DeleteTempFiles(tempEFilePath);
//清除临时文件
//} catch
// //(Exception ex)
//{
// rp.IsError = true;
// rp.Message = "EXCEL文件已填充完毕,但在保存时发生错误"; return rp;
//}
//rp.Message = tempEFileXPath; return rp;
#endregion
} catch (Exception ex) {
rp.IsError = true;
rp.Message = ex.ToString();
return rp;
}
finally
{ ExcelDispose(app); }
}
/// /// 关闭Excel进程 /// /// /// ///
}}
protected void ExcelDispose(_Application CurExcel)
{
try
{
if (CurExcel != null)
{
CurExcel.Workbooks.Close();
CurExcel.Quit();
#region
//强行杀死打开的Excel进程
IntPtr excelIPtr = new IntPtr(CurExcel.Hwnd);
//得到这个句柄,具体作用是得到这块内存入口 int proID
}
}
#ednregion
using Excel;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Configuration;
public partial class UcTaskList : Tobto.DJ.DJWebUI.UIClass.DJUcListPageBase
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
private ResultPacket ToExcel()
{
ResultPacket rp = new ResultPacket();
string excelTemplateDPath;
//EXCEL模板默认服务器物理存放路径
string tempEFilePath = " //TempFiles";
//EXCEL临时文件保存服务器物理存放路径
string tempEFileXPath = "/TempFiles";
//EXCEL临时文件保存服务器虚拟存放路径
Excel.Application app = null;
//EXCEL对象
Workbooks workbooks;
//工作簿集合
_Workbook workbook;
//当前工作簿
Sheets sheets;
//SHEET页集合
_Worksheet worksheet;
//当前SHEET页 #region 读取配置文件中路线模板路径及名称并验证是否存在
//获取配置文件中路线模板路径及名称
excelTemplateDPath = Server.MapPath(ConfigurationManager.AppSettings["ExcelTemplateDPath"]);
tempEFileXPath = ConfigurationManager.AppSettings["ExcelTemplateDPath"] + tempEFileXPath;
tempEFilePath = excelTemplateDPath + tempEFilePath; excelTemplateDPath += "//TaskTemplet.xls";
//验证EXCEL模板文件是否存在 if (!File.Exists(excelTemplateDPath)) { rp.IsError = true; rp.Message = "配置文件中指定的模板文件不存在"; return rp; }
//验证EXCEL临时文件夹是否存在
if (!File.Exists(tempEFilePath))
{ Directory.CreateDirectory(tempEFilePath); }
#endregion
#region
//从配置文件中取列名为填充数据做准备 ArrayList list = new ArrayList(); if (ConditionArr != null && ConditionArr.Count > 0) { list = TaskManageBrl.Find(ConditionArr[0], ConditionArr[1], ConditionArr[2], ConditionArr[3], bool.Parse(ConditionArr[4]), ConditionArr[5], ConditionArr[6], ConditionArr[7], 0, 10000, true, CurrentLoginUserInfo); } #endregion try { #region 启动excel进程并加载模板
//启动EXCEL进程
app = new Excel.Application();
if (app == null)
{
rp.IsError = true; rp.Message = "Excel进程启动出错,请确认是否引用EXCEL组件";
return rp;
}
app.Visible = false;
app.UserControl = true;
app.DisplayAlerts = false;
//加载读取模板
workbooks = app.Workbooks;
workbook = workbooks.Add(excelTemplateDPath);
sheets = workbook.Worksheets; worksheet = (_Worksheet)sheets.get_Item(1);
#endregion
#region
//填充数据
for (int i = 3; i < list.Count; i++)
{
worksheet.Cells[i, 1] = "Emon任务"; worksheet.Cells[i, 2] = "343";
worksheet.Cells[i, 3] = "233";
}
#endregion
#region 保存临时文件 worksheet.Columns.AutoFit();
//设置自动列宽
try {
string tempFileName = "TaskList" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
tempEFilePath += "//" + tempFileName; tempEFileXPath += "/" + tempFileName;
worksheet.SaveAs(tempEFilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, XlSaveAsAccessMode.xlNoChange, Missing.Value, Missing.Value, Missing.Value);
DeleteTempFiles(tempEFilePath);
//清除临时文件
//} catch
// //(Exception ex)
//{
// rp.IsError = true;
// rp.Message = "EXCEL文件已填充完毕,但在保存时发生错误"; return rp;
//}
//rp.Message = tempEFileXPath; return rp;
#endregion
} catch (Exception ex) {
rp.IsError = true;
rp.Message = ex.ToString();
return rp;
}
finally
{ ExcelDispose(app); }
}
/// /// 关闭Excel进程 /// /// /// ///
}}
protected void ExcelDispose(_Application CurExcel)
{
try
{
if (CurExcel != null)
{
CurExcel.Workbooks.Close();
CurExcel.Quit();
#region
//强行杀死打开的Excel进程
IntPtr excelIPtr = new IntPtr(CurExcel.Hwnd);
//得到这个句柄,具体作用是得到这块内存入口 int proID
}
}
#ednregion
相关文章推荐
- C#实现Excel模板导出和从Excel导入数据
- C#导出数据到Excel模板and如何画Excel模板并导出数据,导入excel数据
- C#实现EXCEL数据的导入导出
- EXT2.2+C#.net+Sql 2005实现将数据导入Excel保存
- C#导入导出数据到Excel的通用类代码
- [转]C#实现MS SQL数据导出到Excel
- thinkphp实现excel数据的导入导出(附完整案例)
- Thinkphp框架下PHPExcel实现Excel数据的批量化导入导出
- c# Winform程序实现多sheet的Excel文件导入与导出
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- C# 通过NPOI导入导出数据EXCEL
- C# 操作EXCEL 类 可以实现导入导出功能 折腾了好长时间 现在两种方法显示出来
- c#实现excel数据高效导入到sql数据库
- 使用POI实现在java程序中导入导出Excel文件数据
- C# Winform Excel的导出,根据excel模板导出数据
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- 利用DataGridView及Excel实现数据的导入导出备份
- 轻松实现SQL Server与Access、Excel数据表间的导入导出
- asp.net/c#代码实现excel to mssql数据导入