我有一个返回csv行的方法。但是我想将其转换为数据表。意思,无论在哪里,","我想将数据放入新的列单元格。
private static string ConvertGridViewRowToCsvRow(GridViewRow row)
{
StringBuilder csvRow = new StringBuilder();
bool firstCell = true;
foreach (DataControlFieldCell cell in row.Cells)
{
string text = string.Empty;
if (!String.IsNullOrEmpty(cell.Text))
{
text = cell.Text;
}
else if (cell.Controls.Count > 0)
{
foreach (var control in cell.Controls)
{
if (control is ITextControl)
{
var textControl = control as ITextControl;
text += HttpUtility.HtmlDecode(textControl.Text).Replace("\r", string.Empty).Replace("\n", string.Empty).Trim();
}
else if (control is TextImage)
{
var textImage = control as TextImage;
text += HttpUtility.HtmlDecode(textImage.Text).Replace("\r", string.Empty).Replace("\n", string.Empty).Trim();
}
}
}
if (!firstCell)
{
csvRow.Append(",");
}
csvRow.Append(MakeTextCsvFriendly(text));
firstCell = false;
}
return csvRow.ToString();
}
这是在调用ConvertGridViewRowToCsvRow
public static void Export(string fileName, GridView gv, HashSet<string> selectedRows)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "text/csv";
//SetHiddenColumnsVisibility(gv); enable if you want only selected columns to visible on export (not current business requirement)
var csvFile = new StringBuilder();
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
if (gv.HeaderRow != null)
{
GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
bool firstCell = true;
foreach (DataControlFieldHeaderCell cell in gv.HeaderRow.Cells)
{
if (!firstCell)
{
csvFile.Append(",");
}
string text = cell.ContainingField.HeaderText;
csvFile.Append(MakeTextCsvFriendly(text));
firstCell = false;
}
csvFile.Append("\r\n");
}
if (selectedRows.Count > 0)
{
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow && selectedRows.Contains(gv.DataKeys[row.RowIndex].Value.ToString()))
{
GridViewExportUtil.PrepareControlForExport(row);
csvFile.AppendLine(ConvertGridViewRowToCsvRow(row));
}
}
}
else
{
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
csvFile.AppendLine(ConvertGridViewRowToCsvRow(row));
}
}
// render the htmlwriter into the response
HttpContext.Current.Response.Write(csvFile.ToString());
HttpContext.Current.Response.End();
}
}
}
我在想一个替换csvFile.Append(",");
的方法,或者可能会使用这样的文件将数据存储在DataTable中的方法。
但是我需要在构建此方法时提供指导。
答案 0 :(得分:-1)
我尝试使用以下代码将csv文件转换为适用于Windows应用程序的数据表。
private void CSVtoDataTable(string filepath)
{
int count = 1;
char fieldSeparator = ',';
DataTable csvData = new DataTable();
using (TextFieldParser csvReader = new TextFieldParser(filePath))
{
csvReader.HasFieldsEnclosedInQuotes = true;
while (!csvReader.EndOfData)
{
csvReader.SetDelimiters(new string[] { "," });
string[] fieldData = csvReader.ReadFields();
if(count==0)
{
foreach (string column in fieldData)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
}
else
{
csvData.Rows.Add(fieldData);
}
}
}
}