异常问题1:
如 机构编号 00001222 导出城CSV后,前面的四个0000不显示了,
解决办法 输出格式变为 ="00001222"
异常问题2:
PPMABAT01:/MABATAPS/usr/ma_batas >CH_INS_ID_CD='00105840' and trans_dt='20101028' fetch first 10 rows only with ur" <
TRANS_DT TRANS_MT
-------- --------
20101028 075823
20101028 075318
20101028 075446
20101028 075637
20101028 075702
解决:(如果要按上面的格式输出,请用下面的方式)
try
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
sb.Append("\"" + HttpContext.Current.Server.HtmlDecode(entityValues[i].ToString().Replace("\"", "\"\"").
Replace("\n", Environment.NewLine).Replace("<BR>", Environment.NewLine)) + "\""); //Replace("\"", "\"\"").
Replace("\n", Environment.NewLine).避免单元格内是SQL脚本情况换的问题,是由于字符串中含有双引号导致的。 HttpContext.Current.Server.HtmlDecode这句话可以不加,这里是应为在文本入库的时候被encode了 - -!
sb.Append(",");
}
catch
{
entityValues[i] = string.Empty;
sb.Append("\"" + entityValues[i].ToString() + "\"");
sb.Append(",");
}
/// <summary>
/// 服务单导出
/// </summary>
public void ServiceOrderExport(string data)
{
StringBuilder sb = new StringBuilder();
Type entityType = null; ;
PropertyInfo[] entityProperties = null;
var input = data.DeserializeObject<structServiceOrder>();
using (var context = SRVDBHelper.DataContext)
{
sb.Remove(0, sb.Length);
var results = context.Usp_SRV_CheckServiceOrder(input.ServiceOrderID, input.AcceptWay,
input.StatusCode, input.Description, input.OneLevelSortID, input.TwoLevelSortID,
input.ThreeLevelSortID, input.AInsNO, input.ACompanyName, input.ADepartmentID,
input.ASectionID, input.AName, input.CInsNO, input.CCompanyName, input.CDepartmentID,
input.CSectionID, input.CreatorName, input.HInsNO, input.HCompanyName, input.HDepartmentID,
input.HSectionID, input.HName, input.CreateDate1, input.CreateDate2,input.FinishDate1,
input.FinishDate2,input.OverDueStatus);
var entitys = results.ToList();
//检查实体集合不能为空
if (entitys == null || entitys.Count < 1)
{
return;
}
//取出第一个实体的所有Propertie
entityType = entitys[0].GetType();
entityProperties = entityType.GetProperties();
for (int i = 0; i < entityProperties.Length; i++)
{
sb.Append(entityProperties[i].Name);
sb.Append(",");
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
//将所有entity添加到DataTable中
foreach (object entity in entitys)
{
//检查所有的的实体都为同一类型
if (entity.GetType() != entityType)
{
throw new Exception("要转换的集合元素类型不一致");
}
object[] entityValues = new object[entityProperties.Length];
for (int i = 0; i < entityProperties.Length; i++)
{
try
{
entityValues[i] = entityProperties[i].GetValue(entity, null);
sb.Append("\"" + entityValues[i].ToString() + "\""); 解决如果列值为空的话,后面的空行不顶上来
sb.Append(",");
}
catch
{
entityValues[i] = string.Empty;
sb.Append("\"" + entityValues[i].ToString() + "\"");
sb.Append(",");
}
}
sb.Remove(sb.Length - 1, 1);
sb.Append("\r\n");
}
HttpResponse resp;
resp = System.Web.HttpContext.Current.Response;
resp.Charset = "GB2312";
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + string.Format("{0:yyyyMMddHHmmss}", DateTime.Now) + ".csv");
resp.Write(sb);
resp.End();
}
}