代码如下:
int intDataCount = myData.Tables[0].Rows.Count;
Microsoft.Office.Interop.Excel.Application app = null; Microsoft.Office.Interop.Excel.Workbook workbook = null; Microsoft.Office.Interop.Excel.Worksheet sheet = null;
object missing = System.Reflection.Missing.Value; // 表示 System.Reflection.Missing 类的唯一实例。 string strPath = string.Empty; string strFileName = string.Empty; try { app = new Microsoft.Office.Interop.Excel.Application(); app.Visible = false; // 不显示,仅后台生成 workbook = app.Workbooks.Add(true); // 如果打开已存在文件,这里用Open,保存使用Save // 添加sheet的方法 workbook.Worksheets.Add(missing, missing, missing, missing); // 修改sheet名的方法,注意sheet的下标从1开始 sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1]; sheet.Name = "new sheet name"; //想要将sheet的名称改为Sheet1,则需改 // 添加sheet的方法 // 注销 workbook.Worksheets.Add(missing, missing, missing, missing); // 中间不变 //sheet.Name = "Sheet1"; //添加标题 app.Cells[1, 1] = strTitle; app.Cells[2, 1] = "项目名称:" + myData.Tables[0].Rows[0][“columnName”].ToString(); //合并单元格 sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, 6]).MergeCells = true; sheet.get_Range(sheet.Cells[2, 1], sheet.Cells[2, 6]).MergeCells = true; //居中对齐 sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, 6]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //设置字体 sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[1, 6]).Font.Size = 13; sheet.get_Range(sheet.Cells[2, 1], sheet.Cells[2, 6]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; // 设置字体和大小 //range = sheet.get_Range(sheet.Cells[1, 1], sheet.Cells[rowCount, columnCount]); //range.Font.Name = “Arial”; //range.Font.Size = 10; // 设置单元格的wrap text属性 sheet.get_Range(sheet.Cells[3, 1], sheet.Cells[intDataCount + 2, 6]).WrapText = true; // 设置单元格的数据格式为文本格式 sheet.get_Range(sheet.Cells[3, 1], sheet.Cells[intDataCount + 2, 6]).NumberFormat = "@"; //设置自动调整列宽 sheet.get_Range(sheet.Cells[3, 1], sheet.Cells[intDataCount + 2, 6]).EntireColumn.AutoFit();
//sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].EntireColumn.ColumnWidth = 20;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].WrapText = false;
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].NumberFormat = "@";
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].EntireRow.AutoFit();
sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
Microsoft.Office.Interop.Excel.Range range; Array arr = Array.CreateInstance(typeof(object), intDataCount + 2, 6); // 不断的调用下面的的函数填充array中的内容 // 行,列均从0开始 arr.SetValue("列名1", 0, 0); arr.SetValue("列名2", 0, 1); arr.SetValue("列名3", 0, 2); arr.SetValue("列名4", 0, 3); arr.SetValue("列名5", 0, 4); arr.SetValue("列名6", 0, 5); int i = 1; foreach (DataRow row in myData.Tables[0].Rows) { arr.SetValue(row["columnName1"], i, 0); arr.SetValue(row["columnName2"], i, 1); arr.SetValue(row["columnName3"], i, 2); arr.SetValue(row["columnName4"], i, 3); arr.SetValue(row["columnName5"], i, 4); arr.SetValue(row["columnName6"], i, 5); i++; } // 设置array数据,注意选择的行数和列数要与array行数和列数对应 //sheet.get_Range(sheet.Cells[3, 1], sheet.Cells[intDataCount + 2, 6]).Value2 = arr; sheet.Range[sheet.Cells[1, 1], sheet.Cells[intDataCount + 1, 13]].Value2 = arr; // 这里给sheet填充内容部分略 // 如果文件已经存在又不想让“是否替换”的提示窗体显示出来,需要先调用File.Delete来删除文件 strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"; strPath = Server.MapPath("~/upload"); workbook.SaveAs(strPath + "\" + strFileName, missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);
HylExcel.NavigateUrl = string.Format(@"{0}/{1}/{2}", PageBase.UrlBase, PageBase.UploadDir, strFileName);
HylExcel.Text = string.Format("<span style='color:red;font-weight:bold;'>点这里下载Excel文件{0}</span>", strFileName);
workbook.Close(false, missing, missing); app.Quit(); workbook = null; app = null; GC.Collect(); //FileInfo fi = new FileInfo(strPath + "\" + strFileName);//excelFile为文件在服务器上的地址 //HttpResponse contextResponse = HttpContext.Current.Response; //contextResponse.Clear(); // contextResponse.Buffer = true; //contextResponse.Charset = "GB2312"; //设置了类型为中文防止乱码的出现 // contextResponse.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", fi.Name)); //定义输出文件和文件名 ///contextResponse.AppendHeader("Content-Length", fi.Length.ToString()); //contextResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //contextResponse.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 //contextResponse.WriteFile(fi.FullName); // contextResponse.Flush(); //fi.Delete(); } catch (Exception ex) { if (app != null) { app.Quit(); app = null; GC.Collect(); } }