如何将数据库中数据再查询出后,写入我已建立好的EXCEL文件的指定位置 我是C#开发的WINFORM 客户端程序呀,不是B/S模式. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 以写文件的方式把DataTable数据导出到Excel /// </summary> /// <param name="dt">DataTable对象</param> /// <param name="strExcelFileName">Excel文件名</param> /// <param name="strError">out参数,返回出错信息</param> /// <param name="isName">是否写标题 true:需要 false:不需要</param> /// <param name="strFileName">标题名称</param> /// <returns> /// -1 出错 /// 0 成功 /// </returns> public static int DataTableToExcel2(DataTable dt, string strExcelFileName,string strFileName,bool isName, out string strError) { strError = ""; int nRet = 0; if (dt == null) { strError = "数据集不能为null"; nRet = -1; return nRet; } if (strExcelFileName.Trim() == string.Empty || strExcelFileName == null) { strError = "文件名不能为空"; nRet = -1; return nRet; } if (isName && strFileName == string.Empty) { strError = "标题名不能为空"; nRet = -1; return nRet; } string strLine = ""; FileStream fs = new FileStream(strExcelFileName, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Unicode); try { if (isName) { sw.WriteLine(strFileName); } for (int i = 0; i < dt.Columns.Count; i++) { strLine += dt.Columns[i].ColumnName.ToString(); } sw.WriteLine(strLine); for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { strLine += dt.Rows[i][j].ToString(); } sw.WriteLine(strLine); } } catch (Exception ex) { strError = ex.Message; return -1; } finally { sw.Close(); fs.Close(); } return 0; } http://blog.csdn.net/fangxinggood/archive/2006/04/08/655313.aspx 怎样把string转换回原来的font类型 C#后台能不能判断DIV是否隐藏 c#如何读取复数矩阵文件(.txt) 傻瓜问题 安装c#IDE的时候出错,朋友们来看看。 怎么判断listView是否有被选中至少一条信息 不想用try catch处理 有这种方法吗? 请Devexpress Winform版的高手进来看看 数据库的调试 在线等!!!!!!!!!!!!!!!!!!!!!!! 急!!! Asp.net AD认证,异常信息:该服务器不可操作。。 数据库的连接 ★★为什么得不到预期的结果 向各位高手请教一道C#题
/// 以写文件的方式把DataTable数据导出到Excel
/// </summary>
/// <param name="dt">DataTable对象</param>
/// <param name="strExcelFileName">Excel文件名</param>
/// <param name="strError">out参数,返回出错信息</param>
/// <param name="isName">是否写标题 true:需要 false:不需要</param>
/// <param name="strFileName">标题名称</param>
/// <returns>
/// -1 出错
/// 0 成功
/// </returns>
public static int DataTableToExcel2(DataTable dt,
string strExcelFileName,string strFileName,bool isName,
out string strError)
{
strError = "";
int nRet = 0; if (dt == null)
{
strError = "数据集不能为null";
nRet = -1;
return nRet;
}
if (strExcelFileName.Trim() == string.Empty || strExcelFileName == null)
{
strError = "文件名不能为空";
nRet = -1;
return nRet;
}
if (isName && strFileName == string.Empty)
{
strError = "标题名不能为空";
nRet = -1;
return nRet;
}
string strLine = ""; FileStream fs = new FileStream(strExcelFileName,
FileMode.Create,
FileAccess.Write); StreamWriter sw = new StreamWriter(fs,
System.Text.Encoding.Unicode); try
{
if (isName)
{
sw.WriteLine(strFileName);
} for (int i = 0; i < dt.Columns.Count; i++)
{
strLine += dt.Columns[i].ColumnName.ToString();
}
sw.WriteLine(strLine); for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
strLine += dt.Rows[i][j].ToString();
}
sw.WriteLine(strLine);
}
}
catch (Exception ex)
{
strError = ex.Message;
return -1;
}
finally
{
sw.Close();
fs.Close();
} return 0;
}