asp.net DataGridViewµ¼³öµ½ExcelµÄÈý¸ö·½·¨
http://hi.baidu.com/%E5%EB%B3%DF%C8%CB%C9%FA/blog/item/58a1540bbf0bb437b1351d9a.html
#region DataGridViewÊý¾ÝÏÔʾµ½Excel
/// <summary>
/// ´ò¿ªExcel²¢½«DataGridView¿Ø¼þÖÐÊý¾Ýµ¼³öµ½Excel
/// </summary>
/// <param name="dgv">DataGridView¶ÔÏó </param>
/// <param name="isShowExcle">ÊÇ·ñÏÔʾExcel½çÃæ </param>
/// <remarks>
/// add com "Microsoft Excel 11.0 Object Library"
/// using Excel=Microsoft.Office.Interop.Excel;
/// </remarks>
/// <returns> </returns>
public bool DataGridviewShowToExcel(DataGridView dgv, bool isShowExcle)
{
if (dgv.Rows.Count == 0)
return false;
//½¨Á¢Excel¶ÔÏó
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//Éú³É×Ö¶ÎÃû³Æ
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//Ìî³äÊý¾Ý
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
}
return true;
}
#endregion
#region DateGridViewµ¼³öµ½csv¸ñʽµÄExcel
/// <summary>
/// ³£Ó÷½·¨£¬ÁÐÖ®¼ä¼Ó\t£¬Ò»ÐÐÒ»ÐÐÊä³ö£¬´ËÎļþÆäʵÊÇcsvÎļþ£¬²»¹ýĬÈÏ¿ÉÒÔµ±³ÉExcel´ò¿ª¡£
/// </summary>
/// <remarks>
/// using System.IO;
/// </remarks>
/// <param name="dgv"></param>
private void DataGridViewToExcel(DataGridView dgv)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "Execl files (*.xls)|*.xls";
dlg.FilterIndex = 0;
dlg.RestoreDirectory = true;
dlg.CreatePrompt = true;
dlg.Title = "±£´æÎªExcelÎļþ";
if (dlg.ShowDialog() == DialogResult.OK)
{
Stream myStream;
myStream = dlg.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding
Ïà¹ØÎĵµ£º
ƽ̨:vs2005,ecc6 ,orcale Êý¾Ý¿â
1.Ê×ÏÈÓÃse37,½¨Á¢Ò»¸ö¶ÁÈ¡º¯Êý,Èçͼ
2.ÏÈÌí¼ÓÈý¸öcom×é¼þInterop.SAPFunctionsOCX.dll,Interop.SAPLogonCtrl.dll,Interop.SAPTableFactoryCtrl.dll
ÏÂÃæµÄ´úÂëÎÒÊÇ×ªÔØÆäËûÍøÕ¾,
ÒÔwebÐÎʽ¶Ôfunction module½øÐе÷ÓÃÓëformÐÎʽ»ù±¾Ò»Ñù,ΨһֵµÃ×¢ÒâµÄµØ·½¾ÍÊÇ:"An ActiveX control ......
public string ClearHtml(string HtmlStr)
{
string tmpStr = HtmlStr;
tmpStr = ReplaceHtml("&#[^>]*;", tmpStr, "");
tmpStr = ReplaceHtml("</?marquee[^>]*>", tmpStr, "");
tmpStr = ReplaceHtml("</?object[^>]*>", tmpStr, "");
tmpSt ......
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Pub µÄժҪ˵Ã÷
/// </summa ......
1¡¢¿ØÖÆGridView¿Ø¼þÖÐÄÚÈݵĻ»ÐÐ
GridView1.Attributes.Add("style","word-break:keep-all;word-wrap:normal");//Õý³£»»ÐÐ
GridView1.Attributes.Add("style","word-break:break-all;word-wrap:break-word");//×Ô¶¯»»ÐÐ
2¡¢É¾³ýGridView¿Ø¼þÐÐÐÅÏ¢µ¯³öÈ·ÈÏÌáʾ¿ò
ÔÚGridViewµÄRowDataBound()ʼþÖÐÌ ......