您的位置:首页 > 其它

net导出excel 并设置格式

2015-09-24 10:00 302 查看
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dgv">DataGridView对象</param>
/// <param name="FileName">文件名, 不带后缀名</param>
public void Export(string FileName, List<IncomeStatementOutputDto> list)
{
Excel.Application excel1 = new Excel.Application();
excel1.DisplayAlerts = false;
Excel.Workbook workbook1 = excel1.Workbooks.Add(Type.Missing);
excel1.Visible = false;
Excel.Worksheet worksheet1 = (Excel.Worksheet)workbook1.Worksheets["sheet1"];
//表头
worksheet1.Cells[1, 1] = "流水号";  //Excel里从第1行,第1列计算
worksheet1.Cells[1, 2] = "日期";
worksheet1.Cells[1, 3] = "对方账户名称";
worksheet1.Cells[1, 4] = "对方银行账户";
worksheet1.Cells[1, 5] = "金额(元)";
worksheet1.Cells[1, 6] = "账户余额(元)";
worksheet1.Cells[1, 7] = "业务说明";

//宽度
((Range)worksheet1.Cells[1, 1]).EntireColumn.ColumnWidth = 20;
((Range)worksheet1.Cells[1, 2]).EntireColumn.ColumnWidth = 20;
((Range)worksheet1.Cells[1, 3]).EntireColumn.ColumnWidth = 20;
((Range)worksheet1.Cells[1, 4]).EntireColumn.ColumnWidth = 20;
((Range)worksheet1.Cells[1, 5]).EntireColumn.ColumnWidth = 15;
((Range)worksheet1.Cells[1, 6]).EntireColumn.ColumnWidth = 15;
((Range)worksheet1.Cells[1, 7]).EntireColumn.ColumnWidth = 20;

//水平位置
((Range)worksheet1.Columns[1]).HorizontalAlignment = XlHAlign.xlHAlignRight;
((Range)worksheet1.Columns[5]).HorizontalAlignment = XlHAlign.xlHAlignRight;
((Range)worksheet1.Columns[6]).HorizontalAlignment = XlHAlign.xlHAlignRight;

((Range)worksheet1.Rows[1]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
((Range)worksheet1.Rows[1]).Font.Bold = true;

for (int i = 0; i < list.Count; i++)
{
IncomeStatementOutputDto model = list[i];
worksheet1.Cells[i + 2, 1] = "'" + model.SwiftNumber;  //Excel里从第1行,第1列计算
worksheet1.Cells[i + 2, 2] = "'" + model.HappenDate;
worksheet1.Cells[i + 2, 3] = model.BankName;
worksheet1.Cells[i + 2, 4] = "'" + model.EachOtherAccountName;
string strMoneyFlg = (_intoStr.Contains(("," + model.OperatingType.ToString() + ",")) ? "+" : "-");
worksheet1.Cells[i + 2, 5] = "'" + strMoneyFlg + model.Money;
if (strMoneyFlg == "+")
{
((Range)worksheet1.Cells[i + 2, 5]).Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
}
else
{
((Range)worksheet1.Cells[i + 2, 5]).Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Green);
}

worksheet1.Cells[i + 2, 6] = model.Balance;
worksheet1.Cells[i + 2, 7] = model.BusinessDescription;
}
string fileName = FileName + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
string filePath = Server.MapPath("~/" + fileName);
workbook1.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excel1.Workbooks.Close();
excel1.Quit();
int generation = GC.GetGeneration(excel1);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel1);
excel1 = null;
GC.Collect(generation);
//打开要下载的文件,并把该文件存放在FileStream中
System.IO.FileStream Reader = System.IO.File.OpenRead(filePath);
//文件传送的剩余字节数:初始值为文件的总大小
long Length = Reader.Length;
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.AddHeader("Content-Length", Length.ToString());
byte[] Buffer = new Byte[10000];    //存放欲发送数据的缓冲区
int ByteToRead;                     //每次实际读取的字节数
while (Length > 0)
{
//剩余字节数不为零,继续传送
if (Response.IsClientConnected)
{
//客户端浏览器还打开着,继续传送
ByteToRead = Reader.Read(Buffer, 0, 10000); //往缓冲区读入数据
HttpContext.Current.Response.OutputStream.Write(Buffer, 0, ByteToRead); //把缓冲区的数据写入客户端浏览器
HttpContext.Current.Response.Flush();   //立即写入客户端
Length -= ByteToRead;   //剩余字节数减少
}
else
{
//客户端浏览器已经断开,阻止继续循环
Length = -1;
}
}
//关闭该文件
Reader.Close();
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: