.NET導(dǎo)出報(bào)表匯總(一):
DATASET導(dǎo)出報(bào)表,,優(yōu)點(diǎn):使用方便,,代碼簡(jiǎn)單,缺點(diǎn):不用為報(bào)表設(shè)置樣式
1,、DATASET導(dǎo)出報(bào)表
protected void Button1_Click(object sender, EventArgs e)
{
// 文件名字定義
string fileName = string.Empty;
// 取得導(dǎo)入的Excel數(shù)據(jù)定義
System.Data.DataTable dtExcelData = null;
// 文件名字的生成
string topic = drpTopic.SelectedIndex > 0 ? drpTopic.SelectedItem.Text : "";
fileName = topic + DateTimePicker1.Text + "到" + DateTimePicker2.Text + "文章訪(fǎng)問(wèn)統(tǒng)計(jì)數(shù)據(jù)_" + System.DateTime.Now.ToString().Replace("/", "_").Substring(0, 10);
string filePath = Server.MapPath("..\\ExcelData\\") + fileName;
try
{
dtExcelData = GetData();
// 數(shù)據(jù)有無(wú)判斷
if (dtExcelData == null || dtExcelData.Rows.Count == 0)
{
return;
}
string directoryPath = filePath;
if (Directory.Exists(directoryPath))
{//do nothing
}
else
{
Directory.CreateDirectory(directoryPath);
}
HttpResponse resp;
resp = System.Web.HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName) + ".xls");
string colHeaders = "", ls_item = "";
int i = 0;
DataRow[] myRow = null;
if (0 < dtExcelData.Rows.Count)
{
myRow = dtExcelData.Select("");
}
for (i = 0; i < dtExcelData.Columns.Count; i++)
{
colHeaders += dtExcelData.Columns[i].Caption.ToString() + "\t";
}
colHeaders += "\n";
resp.Write(colHeaders);
// Excel的數(shù)據(jù)部分生成
foreach (DataRow row in myRow)
{
ls_item = "";
for (i = 0; i < row.Table.Columns.Count; i++)
{
ls_item += row[i].ToString().Replace("\r", "").Replace("\n", "") + "\t";
}
ls_item += "\n";
resp.Write(ls_item);
}
File.Delete(fileName + ".xls");
resp.End();
//應(yīng)該修改為:HttpContext.Current.ApplicationInstance.CompleteRequest();
}
catch (Exception ex)
{
throw ex;
}
}
2、DATASET導(dǎo)出報(bào)表
protected void btnExport_Click(object sender, EventArgs e)
{
string excel_Year = ddl1.SelectedItem.Value;
string excel_Month = ddl2.SelectedItem.Value;
string strExcel_CanalId = ddlCanal.SelectedItem.Value;
int excel_CanalId = -1;
if (string.IsNullOrEmpty(excel_Year))
{
Jss.WindowAlert(this.Page, "導(dǎo)出數(shù)據(jù)失??!", false);
return;
}
else if (string.IsNullOrEmpty(excel_Month))
{
Jss.WindowAlert(this.Page, "導(dǎo)出數(shù)據(jù)失?。?, false);
return;
}
else if (!int.TryParse(strExcel_CanalId, out excel_CanalId))
{
Jss.WindowAlert(this.Page, "導(dǎo)出數(shù)據(jù)失?。?, false);
return;
}
DataSet ds = new DataSet();
System.Data.DataTable dtExcelData = new DataTable();
DataTable tsolved = new DataTable(); //最終數(shù)據(jù)的載體,。
ds = dalRT.GetDataSet(excel_Year, excel_Month, excel_CanalId);
if (ds == null)
{
Jss.WindowAlert(this, "當(dāng)前沒(méi)有可以導(dǎo)出的數(shù)據(jù),!", false);
return;
}
tsolved = ds.Tables[0];
dtExcelData.Columns.Add("序號(hào)");
dtExcelData.Columns.Add("組別");
dtExcelData.Columns.Add("網(wǎng)絡(luò)渠道");
dtExcelData.Columns.Add("套餐版本");
dtExcelData.Columns.Add("端口單價(jià)");
dtExcelData.Columns.Add("使用人");
dtExcelData.Columns.Add("分行電話(huà)");
dtExcelData.Columns.Add("開(kāi)通人手機(jī)號(hào)");
dtExcelData.Columns.Add("端口用戶(hù)名");
dtExcelData.Columns.Add("開(kāi)通方式");
dtExcelData.Columns.Add("備注");
if (tsolved.Rows.Count > 0)
{
for (int i = 0; i < tsolved.Rows.Count; i++)
{
DataRow dr = dtExcelData.NewRow();
dr[0] = tsolved.Rows[i]["Id"];
dr[1] = tsolved.Rows[i]["Groups"];
dr[2] = tsolved.Rows[i]["Canal_Name"];
dr[3] = tsolved.Rows[i]["Product_Name"];
dr[4] = tsolved.Rows[i]["Port_Price"];
dr[5] = tsolved.Rows[i]["Staff_Name"];
dr[6] = tsolved.Rows[i]["Groups_TelPhone_Number"];
dr[7] = tsolved.Rows[i]["Mobile_Number"];
dr[8] = tsolved.Rows[i]["Port_UserName"];
dr[9] = tsolved.Rows[i]["Status"];
dr[10] = tsolved.Rows[i]["Notes"];
dtExcelData.Rows.Add(dr);
}
}
else
{
Jss.WindowAlert(this, "當(dāng)前沒(méi)有可以導(dǎo)出的數(shù)據(jù)!", false);
return;
}
// 文件名字定義
string fileName = string.Empty;
// 文件名字的生成
fileName = System.DateTime.Now.ToString().Replace("/", "_").Substring(0, 7)+"_網(wǎng)站表";
string filePath = Server.MapPath("ExcelData\\") + fileName;
try
{
// 數(shù)據(jù)有無(wú)判斷
if (dtExcelData == null || dtExcelData.Rows.Count == 0)
{
return;
}
string directoryPath = filePath;
if (Directory.Exists(directoryPath))
{//do nothing
}
else
{
Directory.CreateDirectory(directoryPath);
}
HttpResponse resp;
resp = System.Web.HttpContext.Current.Response;
resp.Clear();
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName) + ".xls");
string colHeaders = "", ls_item = "";
int i = 0;
DataRow[] myRow = null;
//
if (0 < dtExcelData.Rows.Count)
{
myRow = dtExcelData.Select("");
}
for (i = 0; i < dtExcelData.Columns.Count; i++)
{
colHeaders += dtExcelData.Columns[i].Caption.ToString() + "\t";
}
colHeaders += "\n";
resp.Write(colHeaders);
// Excel的數(shù)據(jù)部分生成
foreach (DataRow row in myRow)
{
ls_item = "";
for (i = 0; i < row.Table.Columns.Count; i++)
{
ls_item += row[i].ToString().Replace("\r", "").Replace("\n", "") + "\t";
}
ls_item += "\n";
resp.Write(ls_item);
}
File.Delete(fileName + ".xls");
resp.End();
}
catch (Exception ex)
{
throw ex;
}
}