エクスポートのクリックを使用してデータベースからデータレコードをエクスポートしようとするたびに、私は&
のような面白いデータレコードを取得しています。しかし、時にはそれは完璧に動作します。誰が私になぜそれがそうであると言うことができますか、どのように問題を解決することができますか?GridViewから "&"をエクスポートするのを防ぐには?
サンプルコード:
protected void CsvImg_Click(object sender, ImageClickEventArgs e)
{
try
{
DataTable dataTable = (DataTable)Session["dataTable"];
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "sqlresult.csv"));
Response.ContentType = "application/text";
exportView.AllowPaging = false;
exportView.DataSource = dataTable;
exportView.DataBind();
System.Text.StringBuilder strbldr = new System.Text.StringBuilder();
for (int i = 0; i < exportView.HeaderRow.Cells.Count; i++)
{
//separting header columns text with comma operator
strbldr.Append(exportView.HeaderRow.Cells[i].Text + ',');
}
//appending new line for gridview header row
strbldr.Append("\n");
for (int j = 0; j < exportView.Rows.Count; j++)
{
for (int i = 0; i < exportView.HeaderRow.Cells.Count; i++)
{
//separating gridview columns with comma
strbldr.Append(exportView.Rows[j].Cells[i].Text + ',');
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Write(strbldr.ToString());
Response.End();
}
catch (Exception)
{
}
}
インポートコード:
using (CsvFileReader reader = new CsvFileReader(CourseDataFileUpload.PostedFile.InputStream))
{
#region create dt
DataTable List = new DataTable();
List.Columns.Add("CourseID");
List.Columns.Add("CourseName");
List.Columns.Add("CourseCoordinator");
#endregion
CsvRow row = new CsvRow();
bool dataformat = false;
string checkname = "CourseID,CourseName,CourseCoordinator";
while (reader.ReadRowSpecial(row))
{
for (int i = 0; i < row.Count; i++)
{
string total = HttpUtility.HtmlEncode(row[i].ToString());
if (total == checkname)
{
dataformat = true;
}
if (dataformat == true)
{
string[] splitname = total.Split(',');
#region split string
string first = splitname[0].ToString();
string second = splitname[1].ToString();
string third = splitname[2].ToString();
#endregion
List.Rows.Add(first, second, third);
listOfCourseInformation = List;
}
}
if (dataformat == false)
{
break;
}
}
if (List.Rows.Count > 0)
{
List.Rows.RemoveAt(0);
}
}
CSVクラス:
public bool ReadRowSpecial(CsvRow row)
{
row.LineText = ReadLine();
if (String.IsNullOrEmpty(row.LineText))
return false;
int pos = 0;
int rows = 0;
while (pos < row.LineText.Length)
{
string value;
// Special handling for quoted field
if (row.LineText[pos] == '"')
{
// Skip initial quote
//pos++;
// Parse quoted value
int start = pos;
while (pos < row.LineText.Length)
{
// Test for quote character
if (row.LineText[pos] == '"')
{
// Found one
pos++;
// If two quotes together, keep one
// Otherwise, indicates end of value
if (pos >= row.LineText.Length || row.LineText[pos] != '"')
{
pos--;
break;
}
}
pos++;
}
value = row.LineText.Substring(start, pos - start);
value = value.Replace("\"\"", "\"");
}
else
{
//Parse unquoted value
int start = pos;
while (pos < row.LineText.Length && row.LineText[pos] != '"')
pos++;
value = row.LineText.Substring(start, pos - start);
}
// Add field to list
if (rows < row.Count)
row[rows] = value;
else
row.Add(value);
rows++;
// Eat up to and including next comma
while (pos < row.LineText.Length && row.LineText[pos] != '"')
pos++;
if (pos < row.LineText.Length)
pos++;
}
//// Delete any unused items
//while (row.Count > rows)
// row.RemoveAt(rows);
// Return true if any columns read
return (row.Count > 0);
}
もう質問がありますか?データのインポートはどうですか?私は同じ問題を抱えています。コードを更新してみましょう。 –
逆の使用 HttpUtility.HtmlEncode – PraveenVenu
私はそれらを置くべきですか?とにかく私はそれをエンコードしようとすると、 "&"に "&"を回しても、まだインポートに失敗します。 –