当我们输出到EXCEL的值中有 空字符串的时候,由于输出时格式并不确定,因此系统会为空字符串自动输出成一个 ’(单引号),这样输出后的格式就会乱掉,采取一种简单的做法后可以解决。
输出的时候将所有空值 和空字符串 都转成 " " 空格字符串,于是格式乱的问题都解决了。
附: E8提供BaseTools中的关于EXCEL 输出的 代码:
ExportExcel.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Epower.DevBase.BaseTools
{
/**//// <summary>
///
/// </summary>
public class IDataFieldProcess
{
/**//// <summary>
///
/// </summary>
public IDataFieldProcess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 处理字段结果
/// </summary>
/// <param name="lngID"></param>
/// <param name="lngAppID"></param>
/// <param name="lngOpID"></param>
/// <returns></returns>
public virtual string GetDataFieldProcess(string sFieldValue, string sPara)
{
return sFieldValue;
}
}
/**//// <summary>
/// ExcelProc 的摘要说明。
/// </summary>
public class ExportExcel
{
private static OleDbConnection _oleConn;
private static OleDbCommand _oleCmdSelect;
public ExportExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static string AddWithComma(string strSource,string strAdd)
{
if (strSource !="") strSource = strSource += ", ";
return strSource + strAdd;
}
private static OleDbDataAdapter SetSheetQueryAdapter(DataTable dt)
{
// Deleting in Excel workbook is not possible
//So this command is not defined
try
{
OleDbDataAdapter oleda = new OleDbDataAdapter(_oleCmdSelect);
string strInsertPar="";
string strInsert="";
for (int iCol=0;iCol<dt.Columns.Count;iCol++)
{
strInsert= AddWithComma(strInsert,dt.Columns[iCol].ColumnName);
strInsertPar= AddWithComma(strInsertPar,"?");
}
string strTable = "[Sheet1$]";
strInsert = "INSERT INTO "+ strTable + "(" + strInsert +") Values (" + strInsertPar + ")";
oleda.InsertCommand = new OleDbCommand(strInsert,_oleConn);
OleDbParameter oleParIns = null;
for (int iCol=0;iCol<dt.Columns.Count;iCol++)
{
oleParIns = new OleDbParameter("?",dt.Columns[iCol].DataType.ToString());
oleParIns.SourceColumn =dt.Columns[iCol].ColumnName;
oleda.InsertCommand.Parameters.Add(oleParIns);
oleParIns=null;
}
return oleda;
}
catch (Exception ex)
{
throw ex;
}
}
/**//// <summary>
///
/// </summary>
/// <param name="tmpFileName">源文件</param>
/// <param name="toFileName">目标文件</param>
/// <param name="arrFields">字段数组</param>
/// <param name="dtSource">源数据</param>
public static void InsertExcel(string tmpFileName,string toFileName,string[] arrFields,System.Data.DataTable dtSource,IDataFieldProcess idfp)
{
string Tablename = "[Sheet1$]";
System.IO.File.Copy(tmpFileName,toFileName,true);
System.IO.FileInfo fi=new System.IO.FileInfo(toFileName);
fi.Attributes=System.IO.FileAttributes.Normal;
string strCon = @" Provider = Microsoft.Jet.OLEDB.4.0;Data Source= " + toFileName + ";Extended Properties=Excel 8.0";
_oleConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM "+ Tablename+" WHERE 1=2";
try
{
_oleConn.Open () ;
//打开数据链接,得到一个数据集
_oleCmdSelect =new OleDbCommand(strCom, _oleConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , _oleConn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(myCommand);
//创建一个 DataTable对象
DataTable myDT = new DataTable (Tablename) ;
//得到自己的DataSet对象
myCommand.Fill(myDT) ;
foreach(DataRow drSource in dtSource.Rows)
{
DataRow dr = myDT.NewRow();
for(int n=0;n<arrFields.Length;n++)
{
string sFileName=arrFields[n].ToString();
string sValue = "";
if (sFileName.IndexOf(":") > 0)
{
//如果字段名内有: 分开,则经过 一次处理后输出
string[] sArr = sFileName.Split(":".ToCharArray());
sValue = idfp.GetDataFieldProcess(drSource[sArr[0]].ToString(), sArr[1]);
}
else
{
sValue = drSource[sFileName].ToString();
}
//防止空值打乱格式
if (sValue == "")
{
sValue = " ";
}
dr[n] = (object)sValue;
}
myDT.Rows.InsertAt(dr,0);
}
OleDbDataAdapter oleAdapter = SetSheetQueryAdapter(myDT);
oleAdapter.Update(myDT);
}
catch(Exception er)
{
throw new Exception(er.Message);
}
finally
{
_oleConn.Close () ;
}
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
namespace Epower.DevBase.BaseTools
{
/**//// <summary>
///
/// </summary>
public class IDataFieldProcess
{
/**//// <summary>
///
/// </summary>
public IDataFieldProcess()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/**//// <summary>
/// 处理字段结果
/// </summary>
/// <param name="lngID"></param>
/// <param name="lngAppID"></param>
/// <param name="lngOpID"></param>
/// <returns></returns>
public virtual string GetDataFieldProcess(string sFieldValue, string sPara)
{
return sFieldValue;
}
}
/**//// <summary>
/// ExcelProc 的摘要说明。
/// </summary>
public class ExportExcel
{
private static OleDbConnection _oleConn;
private static OleDbCommand _oleCmdSelect;
public ExportExcel()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static string AddWithComma(string strSource,string strAdd)
{
if (strSource !="") strSource = strSource += ", ";
return strSource + strAdd;
}
private static OleDbDataAdapter SetSheetQueryAdapter(DataTable dt)
{
// Deleting in Excel workbook is not possible
//So this command is not defined
try
{
OleDbDataAdapter oleda = new OleDbDataAdapter(_oleCmdSelect);
string strInsertPar="";
string strInsert="";
for (int iCol=0;iCol<dt.Columns.Count;iCol++)
{
strInsert= AddWithComma(strInsert,dt.Columns[iCol].ColumnName);
strInsertPar= AddWithComma(strInsertPar,"?");
}
string strTable = "[Sheet1$]";
strInsert = "INSERT INTO "+ strTable + "(" + strInsert +") Values (" + strInsertPar + ")";
oleda.InsertCommand = new OleDbCommand(strInsert,_oleConn);
OleDbParameter oleParIns = null;
for (int iCol=0;iCol<dt.Columns.Count;iCol++)
{
oleParIns = new OleDbParameter("?",dt.Columns[iCol].DataType.ToString());
oleParIns.SourceColumn =dt.Columns[iCol].ColumnName;
oleda.InsertCommand.Parameters.Add(oleParIns);
oleParIns=null;
}
return oleda;
}
catch (Exception ex)
{
throw ex;
}
}
/**//// <summary>
///
/// </summary>
/// <param name="tmpFileName">源文件</param>
/// <param name="toFileName">目标文件</param>
/// <param name="arrFields">字段数组</param>
/// <param name="dtSource">源数据</param>
public static void InsertExcel(string tmpFileName,string toFileName,string[] arrFields,System.Data.DataTable dtSource,IDataFieldProcess idfp)
{
string Tablename = "[Sheet1$]";
System.IO.File.Copy(tmpFileName,toFileName,true);
System.IO.FileInfo fi=new System.IO.FileInfo(toFileName);
fi.Attributes=System.IO.FileAttributes.Normal;
string strCon = @" Provider = Microsoft.Jet.OLEDB.4.0;Data Source= " + toFileName + ";Extended Properties=Excel 8.0";
_oleConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM "+ Tablename+" WHERE 1=2";
try
{
_oleConn.Open () ;
//打开数据链接,得到一个数据集
_oleCmdSelect =new OleDbCommand(strCom, _oleConn);
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , _oleConn);
OleDbCommandBuilder cb=new OleDbCommandBuilder(myCommand);
//创建一个 DataTable对象
DataTable myDT = new DataTable (Tablename) ;
//得到自己的DataSet对象
myCommand.Fill(myDT) ;
foreach(DataRow drSource in dtSource.Rows)
{
DataRow dr = myDT.NewRow();
for(int n=0;n<arrFields.Length;n++)
{
string sFileName=arrFields[n].ToString();
string sValue = "";
if (sFileName.IndexOf(":") > 0)
{
//如果字段名内有: 分开,则经过 一次处理后输出
string[] sArr = sFileName.Split(":".ToCharArray());
sValue = idfp.GetDataFieldProcess(drSource[sArr[0]].ToString(), sArr[1]);
}
else
{
sValue = drSource[sFileName].ToString();
}
//防止空值打乱格式
if (sValue == "")
{
sValue = " ";
}
dr[n] = (object)sValue;
}
myDT.Rows.InsertAt(dr,0);
}
OleDbDataAdapter oleAdapter = SetSheetQueryAdapter(myDT);
oleAdapter.Update(myDT);
}
catch(Exception er)
{
throw new Exception(er.Message);
}
finally
{
_oleConn.Close () ;
}
}
}
}
ExportExcel.cs 里面 就有专门的处理这个情况。E8.Net用户可以直接参考这个代码进行修正
其中 IDataFieldProcess 主要是为了处理输出EXCEL时 一些字段需要转换的情况(如:状态名称等),如果全在SQL SERVER进行转换会加大 SQLSERVER的压力