首页 > 代码库 > excel 下载
excel 下载
public string CreateExcel(string SelectedBizType, string strReportDate, DropDownList ddlYQ, DropDownList ddlDataTable, ref string strFilePath)
{
Application m_objExcel = new Application();
int pid;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out pid);
Workbooks m_objBooks = (Workbooks)m_objExcel.Workbooks;
string strTepPath = string.Empty;
string strTemplate = string.Empty;
if (SelectedBizType == "3a448698-b695-4a5a-b405-df18f44da86f")
{
strTepPath = ConfigurationManager.AppSettings["TRQBBTemplate"];
strTemplate = "天然气报表.xls";
}
else if (SelectedBizType == "969c388f-b0e2-4571-9876-4312b6a74a54")
{
strTepPath = ConfigurationManager.AppSettings["YCBBTemplate"];
strTemplate = "油藏报表.xls";
}
try
{
string strFileDir = strTepPath + strReportDate + @"/";//将导出的excel保存到该路径下
if (System.IO.Directory.Exists(strFileDir))//用来解决产能下载不稳定的问题
{
System.IO.Directory.Delete(strFileDir, true);//如果由于上一次导出未成功,而留下了痕迹,先删除此文件夹,再创建
}
System.IO.Directory.CreateDirectory(strFileDir);//创建导出文件夹
IDataBase oDB = DBFactory.GetDBInstance();
string strYQName = string.Empty;
string strTemplateChild = string.Empty;
//遍历油区
for (int i = 0; i < ddlYQ.Items.Count; i++)
{
strYQName = ddlYQ.Items[i].Text;
//遍历表
for (int j = 0; j < ddlDataTable.Items.Count; j++)
{
string strShortCode = string.Empty;
string strDtName = ddlDataTable.Items[j].Text;
strTemplateChild = "XXXXX.xls";
strShortCode = "XXXXXXXXXXXX";
//查询业务表数据
string strSql = string.Format("select * from " + strShortCode + " where ny=‘{0}‘ and yq=‘{1}‘", strReportDate.Substring(0, 6), strYQName);
System.Data.DataTable dtLast = oDB.GetDataTable(strSql);
if (dtLast.Rows.Count == 0)
{
continue;
}
_Workbook m_objBook = (_Workbook)m_objBooks.Add(strTepPath + strTemplateChild);
Sheets m_objSheets = (Sheets)m_objBook.Worksheets;
_Worksheet m_objSheet = (_Worksheet)(m_objSheets.get_Item(1));
//查询表结构字段
// string strSchama = string.Format(@"select column_name EnKey from user_tab_columns
// where upper(table_name)=upper(‘{0}‘)
// order by COLUMN_ID", strShortCode);
// System.Data.DataTable dtCols = oDB.GetDataTable(strSchama);
// string colNames = string.Empty;
// for (int k = 0; k < dtCols.Rows.Count; k++)
// {
// colNames += dtCols.Rows[k][0].ToString() + ",";
// }
// if (colNames.Length > 0)
// {
// colNames = colNames.Substring(0, colNames.Length - 1);
// }
// DownloadMutiRefineTable ddR = new DownloadMutiRefineTable();
// System.Data.DataTable dtResource = ddR.GetTableSchema(dtLast, colNames);
System.Data.DataTable dtResource = dtLast.Copy();
//将dataTable中的数据写入sheet
int headRows = m_objSheet.UsedRange.Rows.Count;
for (int k = 0; k < dtResource.Rows.Count; k++)
{
for (int m = 0; m < dtResource.Columns.Count; m++)
{
if (dtResource.Columns[m].ColumnName.ToUpper() == "STATUS")
{
continue;
}
m_objSheet.Cells[k + headRows + 1, m + 1] = dtResource.Rows[k][m].ToString();
}
}
Range rCells = m_objSheet.Range[m_objSheet.Cells[headRows, 1],
m_objSheet.Cells[m_objSheet.UsedRange.Rows.Count, m_objSheet.UsedRange.Columns.Count]]; //shiying20140326 用来解决产能下载不稳定的问题
rCells.Borders.LineStyle = XlLineStyle.xlContinuous;
string save_path = strFileDir + ddlYQ.Items[i].Text + "_" + strReportDate + "_" + strTemplateChild;
m_objBook.SaveAs(save_path, XlFileFormat.xlXMLSpreadsheet, null, null, false, false,
XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
m_objBook.Close(false, null, null);
}
}
m_objBooks.Close();
m_objExcel.Application.Quit();
m_objExcel.Quit();
//将得到的excel打包
Crc32 crc = new Crc32();
strFilePath = strTepPath + strReportDate + strTemplate.Replace(".xls", ".zip");
ZipOutputStream zos = new ZipOutputStream(File.Create(strFilePath));
//zos.SetLevel(6);
DirectoryInfo di = new DirectoryInfo(strFileDir);
foreach (FileInfo item in di.GetFiles())
{
FileStream fs = File.OpenRead(item.FullName);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
ZipEntry entry = new ZipEntry(strReportDate + strTemplate.Substring(0, strTemplate.Length - 4) + @"/" + item.Name);
entry.Size = fs.Length;
fs.Close();
crc.Reset();
crc.Update(buffer);
entry.Crc = crc.Value;
zos.PutNextEntry(entry);
zos.Write(buffer, 0, buffer.Length);
}
zos.Finish();
zos.Close();
System.IO.Directory.Delete(strFileDir, true);//删除导出文件夹
}
catch (Exception err)
{
Message = err.Message;
}
return Message;
}
excel 下载