日期:2014-05-17 浏览次数:20954 次
protected void Button3_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
FirstBindData(); ;
try
{
if (conn != null)
{
cmd.Dispose();
conn.Close();
}
//文件路径
String strFilePath = "\\excel\\";
String strFileName = DateTime.Now.ToString("yyyyMMdd") + ".xlsx";
//判断是不是null
if (xlApp == null)
{
Response.Write("<script>alert('创建Excel失败!')</script>");
return;
}
Object ms = Type.Missing;
Microsoft.Office.Interop.Excel.Workbook myBook = xlApp.Workbooks.Add(ms);
Microsoft.Office.Interop.Excel.Worksheet mySheet = myBook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
//自动调整列宽
mySheet.Columns.EntireColumn.ColumnWidth = 15;
for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[1].Columns.Count; j++)
&