高手进来,如何在Asp.net2.0中生成excel文档!
如何在Asp.net2.0中生成excel文档,需要怎样的.net环境?
比如通过点击按钮生成excel表,完成编辑后保存到服务器.最好附上代码.
------解决方案--------------------我在网上找的例子,又根据自己的需求改了改,供参考。
private void ExportBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
string strFileName= " ";
System.Web.UI.WebControls.CheckBox chkExport=null;
Excel.ApplicationClass excel=null;
Excel.Workbook workbook=null;
Excel.Worksheet activeSheet=null;
Excel.Worksheet sheet=null;
strFileName= "平台和项目周总结和计划 ";
if( System.IO.File.Exists( Server.MapPath(strFileName+ ".xls ") ) )
System.IO.File.Delete( Server.MapPath(strFileName+ ".xls ") );
try
{
excel=new Excel.ApplicationClass();
workbook = excel.Workbooks.Add(Type.Missing);
activeSheet=(Excel.Worksheet)workbook.ActiveSheet;
int week=0;
int rowStart=1;
int rowCur=1;
string strItem=null;
int iItem=2;
foreach(DataGridItem oDataGridItem in dgWP.Items)
{
chkExport = (System.Web.UI.WebControls.CheckBox)oDataGridItem.FindControl( "WPCheckBox ");
if(chkExport.Checked==true)
{
string strWeek=((System.Web.UI.WebControls.Label)(oDataGridItem.FindControl( "weekLabel "))).Text;
if(week!=int.Parse(strWeek))//以周为单位分页
{
sheet = (Excel.Worksheet)excel.Sheets.Add(activeSheet,Type.Missing,Type.Missing,Type.Missing);
DateTime dt=Convert.ToDateTime(oDataGridItem.Cells[4].Text);
DateTime dtStart=(int)dt.DayOfWeek==0?dt.AddDays(1):dt.AddDays(6-(int)dt.DayOfWeek);
sheet.Name=dtStart.ToShortDateString()+ "计划 ";
FillExcelTitle(sheet);
week=int.Parse(strWeek);
rowStart=2;
rowCur=2;
}
//填写该计划的详细内容
System.Data.DataTable dtWPContent=wpContent.SelectWPContentbyWPID(oDataGridItem.Cells[1].Text).Tables[0];
if(dtWPContent.Rows.Count> 0)
{
sheet.Cells[rowCur,1]=dtWPContent.Rows[0][ "ProductLineName "].ToString();
for(int i=0;i <dtWPContent.Rows.Count;i++)
{
if(strItem==dtWPContent.Rows[i][ "ProductTypeName "].ToString())
{
sheet.get_Range( "B "+iItem.ToString(), "B "+(rowCur).ToString()).Merge(0);
sheet.get_Range( "C "+iItem.ToString(), "C "+(rowCur).ToString()).Merge(0);
}
else
{
if(strItem==null) strItem=dtWPContent.Rows[i][ "ProductTypeName "].ToString();
sheet.Cells[rowCur,2]=dtWPContent.Rows[i][ "ProductTypeName "].ToString();
sheet.Cells[rowCur,3]=dtWPContent.Rows[i][ "Frequency "].ToString();
strItem=dtWPContent.Rows[i][ "ProductTypeName "].ToString();
iItem=rowCur;
}
sheet.Cells[rowCur,4]=dtWPContent.Rows[i][ "ProductSpec "].ToString();
sheet.Cells[rowCur,5]=dtWPContent.Rows[i][ "Content "].ToString()+ " "+dtWPContent.Rows[i][ "Tester "].ToString();
if(dtWPContent.Rows[i][ "bTempFlag "].ToString()== "1 ")
{
((Excel.Range)sheet.Cells[rowCur,5]).Font.Color=0xFF0000;
((Excel.Range)sheet.Cells[rowCur,5]).Font.Bold=true;
}
sheet.Cells[rowCur,6]=exFunc.FormatDateString(dtWPContent.Rows[i][ "PlanStartTime "].ToString())
+ "- "+exFunc.FormatDateString(dtWPContent.Rows[i][ "PlanEndTime "].ToString());
sheet.Cells[rowCur,7]=exFunc.FormatDateString(dtWPContent.Rows[i][ "ExecStartTime "].ToString())
+ "- "+exFunc.FormatDateString(dtWPContent.Rows[i][ "ExecEndTime "].ToString());
sheet.Cells[rowCur,8]=dtWPContent.Rows[i][ "FinishState "].ToString();
if(dtWPContent.Rows[i][ "FinishState "].ToSt