最近项目中需要导出数据到Excel 并且需要再次导入到另外一个相同结构的数据库中,(两台相同的服务器,并不联网,也不是局域网),本来用HTML数据直接保存到Excel那种到处数据的,可这种方式只适合导出数据查看,再次导入的时候则不可以 会报“不是预期的格式”之类的错误,是因为文件的格式不是真正的Excel 的格式 ,当用OLEDB 获取数据源的时候则报错,没有办法只能用Excel API生成真正Excel格式的文件。
进入正题:
获得Excel数据源
1//命名空间
2using System.Data.OleDb;
3//获得数据源
4public DataTable ExcelDataSourse(string filePath,string sheetName){
5string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse="+filePath+";Extended Properties=Excel 8.0";
6string sql="select * from ["+sheetName+"$]";
7OleDbConnection con=new OleDbConnection(strConn);
8OleDbDataAdapter oada=new OleDbDataAdapter(sql ,strConn);
9DataTable dt=new DataTable();
10oada.File(dt);
11retutn dt;
12}
1//命名空间
2using System.Data.OleDb;
3//获得数据源
4public DataTable ExcelDataSourse(string filePath,string sheetName){
5string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Sourse="+filePath+";Extended Properties=Excel 8.0";
6string sql="select * from ["+sheetName+"$]";
7OleDbConnection con=new OleDbConnection(strConn);
8OleDbDataAdapter oada=new OleDbDataAdapter(sql ,strConn);
9DataTable dt=new DataTable();
10oada.File(dt);
11retutn dt;
12}
将数据源导入到Excel中
数据记录导入到Excel文件中
//添加Excel支持
using Microsoft.Interop.Excel;
public void ExcelImport(DataTable dtSourse,string diskPath)
{
Application excelApp=new Application();
Workbook workbook=null;
Worksheet worksheet=null;
object missVal=System.Type.Missing;
excelApp.Visable=false;
excelApp.DisplayAlerts=false;
//创建空模板
workbook=excelApp.Workbooks.Add(true);
worksheet=(Worksheet)workbook.ActiveSheet;
//为worksheet起名字
worksheet.Name="NewSheet1";
//创建表头(数据库列便于导入)
for(int dcI=0;dcI<dtSourse.Columns.Count;dcI++)
{
DataColumn currDc=dtSourse.Columns[dcI];
worksheet.Cells[1,dcI+1]=currDc.ColumnName;
}
int excelRowIndex=2;
//添加数据
for(int drIndex=0;drIndex<dtSourse.Rows.Count;drIndex++)
{
DataRow dr=dtSourse.Rows[drIndex];
for(int dcIndex=0;dcIndex<dtSourse.Columns.Count;dcIndex++)
{
string Val=dr[dcIndex].ToString();
worksheet.Cells[excelRowIndex,dcIndex+1]=Val;
}
excelRowIndex++;
}
//保存到服务器
worksheet.SaveAs(diskPath,missVal,missVal,missVal,missVal,missVal,missVal,missVal,missVal,missVal);
workbook.Close(false,missVal,missVal);
excelApp.Quit();
excelApp=null;
GC.Collect();
数据记录导入到Excel文件中
//添加Excel支持
using Microsoft.Interop.Excel;
public void ExcelImport(DataTable dtSourse,string diskPath)
{
Application excelApp=new Application();
Workbook workbook=null;
Worksheet worksheet=null;
object missVal=System.Type.Missing;
excelApp.Visable=false;
excelApp.DisplayAlerts=false;
//创建空模板
workbook=excelApp.Workbooks.Add(true);
worksheet=(Worksheet)workbook.ActiveSheet;
//为worksheet起名字
worksheet.Name="NewSheet1";
//创建表头(数据库列便于导入)
for(int dcI=0;dcI<dtSourse.Columns.Count;dcI++)
{
DataColumn currDc=dtSourse.Columns[dcI];
worksheet.Cells[1,dcI+1]=currDc.ColumnName;
}
int excelRowIndex=2;
//添加数据
for(int drIndex=0;drIndex<dtSourse.Rows.Count;drIndex++)
{
DataRow dr=dtSourse.Rows[drIndex];
for(int dcIndex=0;dcIndex<dtSourse.Columns.Count;dcIndex++)
{
string Val=dr[dcIndex].ToString();
worksheet.Cells[excelRowIndex,dcIndex+1]=Val;
}
excelRowIndex++;
}
//保存到服务器
worksheet.SaveAs(diskPath,missVal,missVal,missVal,missVal,missVal,missVal,missVal,missVal,missVal);
workbook.Close(false,missVal,missVal);
excelApp.Quit();
excelApp=null;
GC.Collect();
导出文件后生成到服务器端,为了节省服务器硬盘,利用二进制方式下载后删除源文件
using System.IO;
public void ConvertToStreamDownLoad(string sourseFile)
{
//转换为二进制并删除源文件
if(File.Exists(sourseFile)){
FileStream fs=new FileStream(sourseFile,FileMode.Open,FileAccess.Read);
string fileName=fs.Name;
int byteLength=Convert.ToInt32(fs.Length);
byte[]bytes=new byte[byteLength];
fs.Read(bytes,0,byteLength);
fs.Close();
fs.Dispose();
File.Delete(sourseFile);
//下载文件
HttpContext.Current.Respone.ContentType="application/vnd.ms-excel";
HttpContext.Current.Respone.ContentEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Respone.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)+";");
HttpContext.Current.Respone.BinaryWrite(bytes);
HttpContext.Current.Respone.End();}
}
using System.IO;
public void ConvertToStreamDownLoad(string sourseFile)
{
//转换为二进制并删除源文件
if(File.Exists(sourseFile)){
FileStream fs=new FileStream(sourseFile,FileMode.Open,FileAccess.Read);
string fileName=fs.Name;
int byteLength=Convert.ToInt32(fs.Length);
byte[]bytes=new byte[byteLength];
fs.Read(bytes,0,byteLength);
fs.Close();
fs.Dispose();
File.Delete(sourseFile);
//下载文件
HttpContext.Current.Respone.ContentType="application/vnd.ms-excel";
HttpContext.Current.Respone.ContentEncoding=System.Text.Encoding.UTF8;
HttpContext.Current.Respone.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)+";");
HttpContext.Current.Respone.BinaryWrite(bytes);
HttpContext.Current.Respone.End();}
}