請問怎麼才能將GRIDVIEW中的數據導到EXCEL並由用戶選擇路徑存入客戶端,並在字符型或所有字段值前加入單引號?
求救:
我現在用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);
//}
}
***************請教各位幫忙,實在是搞不定了,萬分感謝!我這沒什積分可以給大家,還請見諒!