我正在尝试生成一个excel,我可以获得excel但是缺少一些数据(实际上是一个单元格内容)。例如:在sql server查询中(它应该显示在excel中的建议列下的一个单元格中):
建议 类型:赞美类别:医生姓名:嘿
然而,在我的Excel中,在列建议下,它只显示我 - 类型:赞美,然后类别:医生姓名:嘿缺失。 也许我的foreach错了或我错误地检索数据的方式。
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnStr"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
protected void GenerateCSV_Click(object sender, EventArgs e)
{
//First file Inpatient
StringBuilder sb = new StringBuilder();
//fill up DT here
string strQuery ="Select * from table AND Date BETWEEN @FDT AND @TDT"
SqlCommand cmd = new SqlCommand(strQuery);
string fromdate = tbStartDate.Text;
DateTime fdt = Convert.ToDateTime(fromdate);
string todate = tbEndDate.Text;
DateTime tdt = Convert.ToDateTime(todate);
cmd.Parameters.AddWithValue("@FDT", fdt);
cmd.Parameters.AddWithValue("@TDT", tdt);
DataTable dt = GetData(cmd);
//end
for (int k = 0; k < dt.Columns.Count; k++)
{
//add separator
sb.Append(dt.Columns[k].ColumnName + ',');
}
//append new line
sb.Append("\r\n");
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
if (dt.Columns[k].ColumnName.Equals("CommentsSuggestions"))
{
//add separator
sb.Append("\"" + dt.Rows[i][k].ToString().Replace(",", ";") + "\"" + ',');
}
else
{
//add separator
sb.Append(dt.Rows[i][k].ToString().Replace(",", ";") + ',');
}
}
//append new line
sb.Append("\r\n");
}
File.WriteAllText(@"C:\ExportCSV\Inpatient.csv", sb.ToString());
答案 0 :(得分:0)
您的查询不应该
Select * from table WHERE Date BETWEEN @FDT AND @TDT
不
Select * from table AND Date BETWEEN @FDT AND @TDT