代码如下:

         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[];
sheet.Name = "new sheet name"; //想要将sheet的名称改为Sheet1,则需改
// 添加sheet的方法
// 注销 workbook.Worksheets.Add(missing, missing, missing, missing);
// 中间不变
//sheet.Name = "Sheet1"; //添加标题
app.Cells[, ] = strTitle;
app.Cells[, ] = "项目名称:" + myData.Tables[].Rows[][“columnName”].ToString();
//合并单元格
sheet.get_Range(sheet.Cells[, ], sheet.Cells[, ]).MergeCells = true;
sheet.get_Range(sheet.Cells[, ], sheet.Cells[, ]).MergeCells = true;
//居中对齐
sheet.get_Range(sheet.Cells[, ], sheet.Cells[, ]).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
//设置字体
sheet.get_Range(sheet.Cells[, ], sheet.Cells[, ]).Font.Size = ;
sheet.get_Range(sheet.Cells[, ], sheet.Cells[, ]).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[, ], sheet.Cells[intDataCount + , ]).WrapText = true;
// 设置单元格的数据格式为文本格式
sheet.get_Range(sheet.Cells[, ], sheet.Cells[intDataCount + , ]).NumberFormat = "@";
//设置自动调整列宽
sheet.get_Range(sheet.Cells[, ], sheet.Cells[intDataCount + , ]).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 + , );
// 不断的调用下面的的函数填充array中的内容
// 行,列均从0开始 arr.SetValue("列名1", , );
arr.SetValue("列名2", , );
arr.SetValue("列名3", , );
arr.SetValue("列名4", , );
arr.SetValue("列名5", , );
arr.SetValue("列名6", , );
int i = ;
foreach (DataRow row in myData.Tables[].Rows)
{
arr.SetValue(row["columnName1"], i, );
arr.SetValue(row["columnName2"], i, );
arr.SetValue(row["columnName3"], i, );
arr.SetValue(row["columnName4"], i, );
arr.SetValue(row["columnName5"], i, );
arr.SetValue(row["columnName6"], i, );
i++;
} // 设置array数据,注意选择的行数和列数要与array行数和列数对应
//sheet.get_Range(sheet.Cells[, ], sheet.Cells[intDataCount + , ]).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();
}
}

最新文章

  1. DeprecatedAttribute vs. ObsoleteAttribute
  2. 第二百七十二、三天 how can I 坚持
  3. JLink 在J-Flash ARM批处理自动下载
  4. DIV+CSS规范命名
  5. 从jdbc到mybatis
  6. 典型的团队VS优秀团队
  7. 基于R树索引的点面关系判断以及效率优化统计
  8. angular学习笔记03 理论加实践
  9. CentOS下RabbitMq高可用集群环境搭建
  10. opencv人脸检测,旋转处理
  11. CSS存在形式的引用
  12. IO密集型 计算密集型
  13. html2pdf后逐页固定位置盖公章
  14. Nginx和PHP上传文件大小的限制
  15. js代码点击触发事件
  16. Apache 启动失败没有错误信息的解决办法
  17. libevent简述
  18. TortoiseGit + msysgit 记住帐号密码方法及使用密匙的方法
  19. JS中函数声明与函数表达式的异同
  20. Unreal Enginer4特性介绍

热门文章

  1. NGINX编译选项
  2. (转)Linux系统排查4——网络篇
  3. nodejs --- crypto实现加密(转)
  4. DP:凑零钱问题/最长非降子序列(C++)
  5. 牛客网Java刷题知识点之内存的划分(寄存器、本地方法区、方法区、栈内存和堆内存)
  6. Java反射机制分析指南
  7. HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】
  8. Scroller类的使用总结
  9. 大话java基础知识一之为什么java的主函数入口必须是public static void
  10. FTL(FreeMarker)基础