protected void butExport_Click(object sender, EventArgs e)
{
try
{
DataTable dt = GetList(StrSql).Tables[0]
ExportExcel excel = new ExportExcel()
string fileName = excel.Export(dt, DropDownList1.SelectedItem.Text, DataName)
string path = Request.PhysicalApplicationPath + "Excel\\" + fileName + ".xls"
DownFile.ResponseFile(Page.Request, Page.Response, fileName + ".xls", path, 1024000)
FileInfo file = new FileInfo(path)
file.Delete()
}
catch (Exception ex)
{
throw new AppException(ex)
}
}
利用MyXls导出Excel
悄迟和/// <summary>
/// 导出Excel
/// </summary>
//旦咐/ <param name="sender"></param>
/// <param name="e"></param>启盯
public string Export(DataTable dt, string xlsName, List<string>dataName)
{
XlsDocument xls = new XlsDocument()
string fileName = DateTime.Now.ToString("yyyyMMddhhmmss")
xls.FileName = fileName
Worksheet sheet = xls.Workbook.Worksheets.Add(xlsName)//Sheet名称
Cells cells = sheet.Cells
Cell cell = cells.Add(1, 1, dataName[0].ToString())
cell.Font.Bold = true
for (int i = 1i <dataName.Counti++)
{
cell = cells.Add(1, i + 1, dataName[i].ToString())
cell.Font.Bold = true
}
for (int i = 0i <dt.Rows.Counti++)
{
for (int j = 0j <dt.Columns.Countj++)
{
cells.Add(i + 2, j + 1, dt.Rows[i][j].ToString())
}
}
string file = System.Web.HttpContext.Current.Server.MapPath("/Excel/")
xls.Save(file)
return fileName
}
文件下载,目前用着还不错
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
BinaryReader br = new BinaryReader(myFile)
try
{
_Response.AddHeader("Accept-Ranges", "bytes")
_Response.Buffer = false
long fileLength = myFile.Length
long startBytes = 0
double pack = 10240//10K bytes
//int sleep = 200 //每秒5次 即5*10K bytes每秒
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' })
startBytes = Convert.ToInt64(range[1])
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())
if (startBytes != 0)
{
//Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength))
}
_Response.AddHeader("Connection", "Keep-Alive")
_Response.ContentType = "application/octet-stream"
_Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8))
br.BaseStream.Seek(startBytes, SeekOrigin.Begin)
int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1
for (int i = 0i <maxCounti++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())))
Thread.Sleep(sleep)
}
else
{
i = maxCount
}
}
}
catch
{
return false
}
finally
{
br.Close()
myFile.Close()
}
}
catch
{
return false
}
return true
}
你看看吧 void CreatXls() {Excel.Application xapp = new Excel.Application() xapp.Application.Workbooks.Add(true) Excel.Worksheet xsheet = (Microsoft.Office.Interop.Excel.Worksheet)xapp.Sheets[1] xsheet.Name = "表名"//表名xsheet.Cells[1, 1] = "姓名" //xsheet.Columns[1].xsheet.Cells[1, 2] = "性别" DataTable dt = Connection.dataTable(General.ConnectionString, CommandType.Text, "")//这一步 不一样这是自己写的方法但你把要查拍扒缓的数据查询放到datatable里面就行了袭模// DataSet ds = new DataSet() // ds.Tables.Add(dt)
for (int i = 0i <dt.Rows.Counti++){xsheet.Cells[(i + 2), 1] = dt.Rows[i]["tname"].ToString() xsheet.Cells[(i + 2), 2] = dt.Rows[i]["tsex"].ToString() for (int j = 1j <= 2j++){//设置第一行即title部分
((Excel.Range)xsheet.Cells[(i + 1), j]).EntireColumn.AutoFit()//自动调整列此册宽((Excel.Range)xsheet.Cells[(i + 1), j]).EntireRow.AutoFit()//自动调整行高}}string filepath = Server.MapPath("~/test.xls")// 文件路径及文件名 xsheet.SaveAs(filepath) xapp.Workbooks.Close() xapp.Quit() System.GC.Collect() }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)