62,046
社区成员
发帖
与我相关
我的任务
分享
protected void btnDownload_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=Sheet1.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.Write("<table border='1'>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>注:请注意大小写、全角与半角、-和_、:和;小区名称必须是:460-00-XXXXX-XXXXX</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>网元名</td>");
Response.Write("<td align='center'>基站名</td>");
Response.Write("<td align='center'>小区名</td>");
Response.Write("<td align='center'>是否为工程小区(默认为否)</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC442</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11441</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC443</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11442</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S14:</td>");
Response.Write("<td align='center'>460-00-29542-11443</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10221 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10222 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center'>CDGBSC445</td>");
Response.Write("<td align='center'>CDGBSC445:S22:</td>");
Response.Write("<td align='center'>460-00-29542-10223 </td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
Response.Write("<tr>");
Response.Write("<td align='center' colspan='4'>以上为试例,请从下行可以填写</td>");
Response.Write("</tr>");
string SqlString = "select * from web_site";//查询表
try
{
DataTable dt = dbc.GetDataTable(SqlString);
foreach (DataRow Row in dt.Rows)
{
Response.Write("<tr>");
Response.Write("<td align='center'>" + Row["wy_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["jz_name"].ToString() + "</td>");
Response.Write("<td align='center'>" + Row["xq_name"].ToString() + "</td>");
Response.Write("<td align='center'>否</td>");
Response.Write("</tr>");
}
}
catch (Exception ex)
{
Response.Write("<script>alert('"+ex.ToString()+"')</script>");
}
Response.Write("</table>");
Response.End();
}
//导出EXCEL示例:
Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Application.Workbooks.Add(true);
excel.Visible = true;
Worksheet sheet = ((Worksheet)excel.ActiveWorkbook.Sheets[1]);
int iRowCount = dataGridView1.Rows.Count;
int iColCount = dataGridView1.ColumnCount;
for (int k = 0; k < iColCount; k++)
{
excel.Cells[1, k + 1] = dataGridView1.Columns[k].HeaderText;
}
for (int i = 0; i < iRowCount; i++)
{
for (int j = 0; j < iColCount; j++)
{
excel.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
if ((1 == j) && ("★未匹配" == dataGridView1.Rows[i].Cells[j].Value))
{
sheet.get_Range(sheet.Cells[i + 2, j + 1], sheet.Cells[i + 2, j + 1]).Font.Color = System.Drawing.Color.Blue.ToArgb();
}
}
}
#region 导入EXCEL
public static System.Data.DataTable ExcelToDS_manual(string filePath)
{
//创建Application对象
Excel.Application xApp = new Excel.ApplicationClass();
xApp.Visible = true;
Excel.Workbook xBook = xApp.Workbooks._Open(filePath,
Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
Excel.Worksheet xSheet = (Excel.Worksheet)xBook.Sheets[1];
int iColCount = 0;
int iRowCount = 0;
System.Data.DataTable dtExcel = new System.Data.DataTable();
//获取列数
string strColName;
for (int iColIndex = 1; ; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2)
{
break;
}
strColName = ((Excel.Range)xSheet.Cells[1, iColIndex]).Value2.ToString();
if (!string.IsNullOrEmpty(strColName))
{
iColCount++;
}
else
{
break;
}
System.Data.DataColumn newColumn = new DataColumn(strColName);
dtExcel.Columns.Add(newColumn);
}
int iNullRowCount = 0;
string strCellText;
for (int iRowIndex = 2; ; iRowIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2)
{
if ((++iNullRowCount) >= 5)
{
break;
}
continue;
}
strCellText = ((Excel.Range)xSheet.Cells[iRowIndex, 1]).Value2.ToString();
if (!string.IsNullOrEmpty(strCellText))
{
iNullRowCount = 0;
}
else
{
if ((++iNullRowCount) >= 5)
{
break;
}
}
System.Data.DataRow newRow = dtExcel.NewRow();
for (int iColIndex = 1; iColIndex <= iColCount; iColIndex++)
{
if (null == ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2)
{
continue;
}
newRow[iColIndex - 1] = ((Excel.Range)xSheet.Cells[iRowIndex, iColIndex]).Value2.ToString();
}
dtExcel.Rows.Add(newRow);
}
xSheet = null;
xBook.Close(Missing.Value, Missing.Value, Missing.Value);
xBook = null;
xApp.Quit();
xApp = null;
return dtExcel;
}
#endregion