請問怎麼才能將GRIDVIEW中的數據導到EXCEL並由用戶選擇路徑存入客戶端,並在字符型或所有字段值前加入單引號?

ssqlily 2009-02-18 03:05:27
求救:
我現在用C#寫了幾段代碼都無法解決這個問題!
如果用數據流存儲,會出現如果字符串類型的數據導入EXCEL前面的001會直接變為1
而用EXCEL存儲,不會有存儲框提示,自動存在了WEB服務器上,也沒任何提示!
如何才能讓用戶選擇性存儲到客戶端? 多謝
方式一:文件流形式(但不能解決0開台的字串問題)
protected void GridViewToExcel(GridView Gdv, string stitle)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(stitle, System.Text.Encoding.UTF8) + ".xls";
Response.AppendHeader("Content-Disposition", filename);
// 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Gdv.AllowPaging = false;
Gdv.DataBind();
Gdv.RenderControl(oHtmlTextWriter);
Response.Output.Write(oStringWriter.ToString());
Response.Flush();
Response.End();
Gdv.AllowPaging = true;
Gdv.PageIndex = 0;
Gdv.DataBind();
}


方式二:(using Excel; 但是隻在服務器端顯示)
public static void CreateExcelReport(DataSet ds, string str_Type)
{
Excel.Application IExcelApp;
Excel._Workbook IExcelWorkBook;
Excel._Worksheet IExcelWorkSheet;
Excel.Range IExcelRange;

string str_FilePath = "";

try
{
//刪除舊檔
if (File.Exists(str_FilePath))
File.Delete(str_FilePath);

IExcelApp = new Excel.Application();
IExcelApp.Visible = false;

//產生新的 Excel Work Book
IExcelWorkBook = (Excel._Workbook)(IExcelApp.Workbooks.Add(Missing.Value));
IExcelWorkSheet = (Excel._Worksheet)IExcelWorkBook.ActiveSheet;

//製作表頭
DataColumn dc;
for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
dc = ds.Tables[0].Columns[i];
IExcelWorkSheet.Cells[1, i + 1] = dc.ColumnName;
}

//內容
int int_Row = 2;
DataRow dr;
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
dr = ds.Tables[0].Rows[j];

for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
{
dc = ds.Tables[0].Columns[i];
IExcelWorkSheet.Cells[int_Row, i + 1] = dr[i].ToString();
}

int_Row = int_Row + 1;
}

//設定 A1~Z1 為粗體
IExcelWorkSheet.get_Range("A1", "Z1").Font.Bold = true;

//設定自動欄寬
IExcelRange = IExcelWorkSheet.get_Range("A1", "Z1");
IExcelRange.EntireColumn.AutoFit();
IExcelApp.Visible = false;
IExcelApp.UserControl = false;



//釋放所有物件及資源
IExcelWorkBook.Close(null, null, null);
IExcelApp.Workbooks.Close();
IExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelWorkBook);
IExcelWorkSheet = null;
IExcelWorkBook = null;
IExcelApp = null;

Response.Redirect("/" + strFile);
}
catch (Exception e)
{
Response.Write(e.Message);
}
}


方式三:(用WEBEXCEL,但還是存在服務器端)
public static void CreateExcelData(GridView gv, string vfilename)
{
Excel.Application IExcelApp;
Excel._Workbook IExcelWorkBook;
Excel._Worksheet IExcelWorkSheet;
Excel.Range IExcelRange;

int iColumn = gv.Columns.Count;
int iRow = gv.Rows.Count;

//try
//{

IExcelApp = new Excel.Application();

//產生新的 Excel Work Book
IExcelWorkBook = (Excel._Workbook)(IExcelApp.Workbooks.Add(Missing.Value));
IExcelWorkSheet = (Excel._Worksheet)IExcelWorkBook.ActiveSheet;

//製作表頭
for (int i = 0; i < iColumn; i++)
{

IExcelWorkSheet.Cells[1, i + 1] = gv.Columns[i].ToString();
}

//內容
int int_Row = 2;

for (int j = 0; j < iRow; j++)
{

for (int i = 0; i < iColumn; i++)
{

if ((gv.Rows[j].Cells[i].Text == null) || (gv.Rows[j].Cells[i].Text == " ") || (gv.Rows[j].Cells[i].Text == ""))
IExcelWorkSheet.Cells[int_Row, i + 1] = " ";
else
IExcelWorkSheet.Cells[int_Row, i + 1] = "'" + gv.Rows[j].Cells[i].Text;
}

int_Row = int_Row + 1;
}

//設定 A1~Z1 為粗體
IExcelWorkSheet.get_Range("A1", "Z1").Font.Bold = true;

//設定自動欄寬
IExcelRange = IExcelWorkSheet.get_Range("A1", "Z1");
IExcelRange.EntireColumn.AutoFit();
IExcelApp.UserControl = true;
IExcelWorkBook.SaveAs(vfilename, Excel.XlFileFormat.xlWorkbookNormal, null, null, false, false, Excel.XlSaveAsAccessMode.xlExclusive, true, false, null, null, null);
//IExcelWorkBook.SaveAs(str_FilePath,Excel.XlFileFormat.xlWorkbookNormal,null,null,false,false,Excel.XlSaveAsAccessMode.xlExclusive,false,false,null,null,null);
IExcelApp.Visible = false;
//釋放所有物件及資源
IExcelWorkBook.Close(null, null, null);
IExcelApp.Workbooks.Close();
IExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelRange);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(IExcelWorkBook);
IExcelWorkSheet = null;
IExcelWorkBook = null;
IExcelApp = null;

// Response.Redirect("/" + strFile);
//}
//catch (Exception e)
//{
// // Response.Write(e.Message);
//}
}


***************請教各位幫忙,實在是搞不定了,萬分感謝!我這沒什積分可以給大家,還請見諒!
...全文
137 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqlily 2009-02-24
  • 打赏
  • 举报
回复
5樓,我試了你提供的方式,但前面的0還是會消失
DMU_WD 2009-02-20
  • 打赏
  • 举报
回复
设置range.NumberFormat="@";//设置单元格为文本格式
ssqlily 2009-02-20
  • 打赏
  • 举报
回复
謝謝大家!1樓的方法我之前有試過,但是寫法太死了,我這個函數是公用的,幾百個GRIDVIEW都要調用,我沒辦法一個個去寫吧?有沒更好的辦法呢?
3樓這個和我的方法一是一樣的呀,解決不了0開始的字串問題,看不出什麼不同,還是我沒看明白?
我希望是調用公司函數,然後在賦值時來判斷當前是否為數值型,單獨不加引號,可以嗎?
多謝各位的回復
浪子-无悔 2009-02-18
  • 打赏
  • 举报
回复
我以前用的一个方法。看看可是你需要的。。
protected void Button1_Click(object sender, EventArgs e)
{
ExportToExcel("application/ms-excel", "学生信息.xls");

}
public void ExportToExcel(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Output.Write(tw.ToString());
Response.Flush();
Response.End();

}
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
EveryCase 2009-02-18
  • 打赏
  • 举报
回复
顶一下吧
starsword 2009-02-18
  • 打赏
  • 举报
回复
用你的方法一就可以了,至于有0开头的字符,可以这样处理:

比如gdv的第i列是有0开头的,给gdv加RowDataBound方法,

protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[i].Attributes.Add("class", "text");
}

这样处理后,0开头的数据导入EXCEL也不会丢失了

记得抓紧给分~~~

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧