csharp: DataTable export to excel,word,csv etc
8152 단어 Datatable
https://exporter.codeplex.com/
/// <summary>
///
/// </summary>
/// <param name="dt"></param>
/// <param name="Response"></param>
/// <param name="filename"></param>
public static void Convertword(DataTable dt, HttpResponse Response, string filename)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".doc");
Response.Charset = "utf-8";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
/// <summary>
///
/// </summary>
/// <param name="dt"></param>
/// <param name="Response"></param>
/// <param name="filename"></param>
public static void Convertexcel(DataTable dt, HttpResponse Response, string filename)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");//
Response.Charset = "utf-8";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
/// <summary>
///
/// </summary>
/// <param name="dataTable"></param>
/// <param name="Response"></param>
/// <param name="filename"></param>
public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".csv");
Response.Charset = "utf-8";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "Application/x-msexcel";
StringBuilder sb = new StringBuilder();
if (dataTable.Columns.Count != 0)
{
foreach (DataColumn column in dataTable.Columns)
{
sb.Append(column.ColumnName + ',');
}
sb.Append("\r
");
foreach (DataRow row in dataTable.Rows)
{
foreach (DataColumn column in dataTable.Columns)
{
if (row[column].ToString().Contains(",") == true)
{
row[column] = row[column].ToString().Replace(",", "");
}
sb.Append(row[column].ToString() + ',');
}
sb.Append("\r
");
}
}
Response.Write(sb.ToString());
Response.End();
//HttpContext.Current.ApplicationInstance.CompleteRequest();
}
winform:
/// <summary>
/// winform
/// Encoding
///
/// </summary>
/// <param name="dgv"></param>
/// <param name="fileName"></param>
public static void ExportToCSV(DataGridView dgv, string fileName)
{
if (dgv.Rows.Count < 1)
{
MessageBox.Show(" !", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.Filter = "CSV (*.csv)|*.csv| (*.txt)|*.txt| (*.*)|*.*";
sfDialog.FilterIndex = 0;
sfDialog.FileName = fileName;
if (sfDialog.ShowDialog() == DialogResult.OK)
{
string strFileName = sfDialog.FileName;
StreamWriter sw = new StreamWriter(strFileName, false, Encoding.Unicode);
string strLine = "";
foreach (DataGridViewColumn col in dgv.Columns)
{
if (col.Visible)
{
strLine += "\"" + col.HeaderText.Trim().Replace("\"", "\\\"") + "\"" + "\t";
}
}
strLine = strLine.Substring(0, strLine.Length - 1);
sw.WriteLine(strLine);
sw.Flush();
foreach (DataGridViewRow dgvr in dgv.Rows)
{
strLine = "";
foreach (DataGridViewCell dgvc in dgvr.Cells)
{
if (dgvc.Visible)
{
if (dgvc.Value == null)
{
strLine += "\t";
}
else
{
strLine += "\"" + dgvc.Value.ToString().Trim().Replace("\"","\"\"") + "\"" + "\t";
}
}
}
sw.WriteLine(strLine);
sw.Flush();
}
sw.Close();
MessageBox.Show(string.Format("
{0}
!", strFileName), " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
yui--datatable 행 추가 형식텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.