如何将数据导入到Excel,方法大体分为两种。一是以数据流的形式写入到文件,另外一种就是调用Microsoft的Excel.dll。 今天主要介绍后者, 如何根据传入数据和显示格式自动的生成Excel。首先看源代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.IO;
using System.Diagnostics;
using Excel;
using System.Runtime.InteropServices;
namespace Gauss.Common
...{
public class GaussExcel
...{
// Fields
private DateTime afterTime;
private DateTime beforeTime;
private int titleColorindex;
private int topPargin;
private int leftPargin;
private bool showBlank;
public bool ShowBlank
...{
get ...{ return showBlank; }
set ...{ showBlank = value; }
}
public int LeftPargin
...{
get ...{ return leftPargin; }
set ...{ leftPargin = value; }
}
public int TopPargin
...{
get ...{ return topPargin; }
set ...{ topPargin = value; }
}
public GaussExcel()
...{
this.titleColorindex = 15;
this.leftPargin = 2;
this.topPargin = 2;
this.showBlank = true;
}
private void ClearFile(string FilePath)
...{
string[] textArray1 = Directory.GetFiles(FilePath);
if (textArray1.Length > 10)
...{
for (int num1 = 0; num1 < 10; num1++)
...{
try
...{
File.Delete(textArray1[num1]);
}
catch
...{
}
}
}
}
public string ProduceExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)
...{
string fileName=null;
ClearFile(FilePath);
fileName = OutputExcel(dt, strTitle, FilePath, nameList);
KillExcelProcess();
return fileName;
}
public void KillExcelProcess()
...{
Process[] processArray1 = Process.GetProcessesByName("Excel");
foreach (Process process1 in processArray1)
...{
DateTime time1 = process1.StartTime;
if ((time1 > this.beforeTime) && (time1 < this.afterTime))
...{
process1.Kill();
}
}
}
public string OutputExcel(System.Data.DataTable dt, string strTitle, string FilePath, Hashtable nameList)
...{
this.beforeTime = DateTime.Now;
//FiledName
int num1 = this.topPargin+1;
if (this.showBlank) num1 += 1;
int numTitle = num1;
int num2 = this.leftPargin-1;
Application application1 = new ApplicationClass();
_Workbook workbook1 = application1.Workbooks.Add(true);
_Worksheet worksheet1 = (_Worksheet)workbook1.ActiveSheet;
foreach (DataColumn column1 in dt.Columns)
...{
bool myFlag = false;
string text5 = column1.ColumnName.Trim();
if (nameList.ContainsKey(text5))
...{
num2++;
application1.Cells[num1, num2] = nameList[text5];
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Font.Bold = true;
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Select();
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Interior.ColorIndex = this.titleColorindex;
}
/**//* IDictionaryEnumerator enumerator1 = nameList.GetEnumerator();
while (enumerator1.MoveNext())
{
if (enumerator1.Key.ToString().Trim() == text5)
{
text5 = enumerator1.Value.ToString() ;
myFlag = true;
}
}
if (myFlag) {
num2++;
application1.Cells[num1, num2] = text5;
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Font.Bold = true;
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Select();
worksheet1.get_Range(application1.Cells[num1, num2], application1.Cells[num1, num2]).Interior.ColorIndex = this.titleColorindex;
} */
}
//Fill In Data To Excel
分享到:
相关推荐
springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel 内容有点多 springboot 导出excel 导入excel 生成excel ...
使用aspose导出Excel(根据模板导出excel)
基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip基于SpringBoot和POI实现单线程和多线程导出Excel.zip...
导出Excel功能,导出Excel功能,导出Excel功能,导出Excel功能
asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总asp.net里导出excel表方法汇总
导出Excel表格 /// 导出Excel /// 创建Excel并写入数据 /// List转化为DataTable /// 转换数据类型
Delphi DBGrid导出Excel2010
本文实例为大家分享了Winform实现导入导出Excel文件的具体代码,供大家参考,具体内容如下 /// /// 导出Excel文件 /// /// /// <param name=dataSet></param> /// 数据集 /// 导出后是否打开文件 /// ...
使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类使用ITEXT导出EXCEL工具类
oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel
delphi dbgrid导出excel单元文件绝对好用 调用方式 var GridtoExcel: TDBGridEhToExcel; begin GridtoExcel := TDBGridEhToExcel.Create(nil); try GridtoExcel.DBGridEh := DBGridEh1; GridtoExcel.FileName :=...
C#导出excel文件
poi基于模板导出excel,poi基于模板导出excelpoi,基于模板导出excel
使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用使用apach poi 导出Excel,可直接在项目中使用
可以运行的POI导出Excel文件实例,里面有两种方法,一个是Servlet,一个是main
cxGrid带正副标题导出Excel
jquery 导出excel方法,开发过程中使用到的几种导出方法的总结。
PHP导出Excel (注:导出带图片的EXCEL也支持),已修正保存到本地不能打开已经乱码的问题,具体内容可参照文件中demo.php有详细说明。
java使用easypoi导入导出excel
本人从网上搜集资料,加上自己研究探索,现在完成了用Jxls导入导出Excel2003和Excel2007数据,读取和写入xls和xlsx文件,现把结果告诉大家,希望大家不走弯路,直接掌握先进实用的技术,解决实际工作问题。...