I am trying to generate an excel, i can get excel but some data (actually one cell content) are missing. For example: In sql server query(it should be shown in one cell under suggestions column like this in excel):
Suggestions Type: Compliment Category:Doctor Name:Hey
However, in my excel, under column Suggestions, it only shows me - Type: Compliment, and then Category:Doctor Name:Hey are missing. maybe my foreach is wrong or the way i retrieve data wrongly.
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());
Shouldn't your query be
Select * from table WHERE Date BETWEEN @FDT AND @TDT
not
Select * from table AND Date BETWEEN @FDT AND @TDT
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.