发布网友 发布时间:2022-05-01 20:20
共5个回答
懂视网 时间:2022-04-27 20:07
1.操作Excel的动态链接库输出路径
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Collections;
/// <summary>
///ExcelHelper 的摘要说明
/// </summary>
public class ExcelHelper
{
private string reportModelPath = null;
private string outPutFilePath = null;
private object missing = Missing.Value;
Excel.Application app;
Excel.Workbook workBook;
Excel.Worksheet workSheet;
Excel.Range range;
/// <summary>
/// 获取或设置报表模板路径
/// </summary>
public string ReportModelPath
{
get { return reportModelPath; }
set { reportModelPath = value; }
}
/// <summary>
/// 获取或设置
/// <summary>
/// 导出数据
/// </summary>
protected void Export_Data()
{
int ii = 0;
//取得报表模板文件路径
string reportModelPath = HttpContext.Current.Server.MapPath("ReportModel/导出订单模板.csv");
//导出报表文件名
fileName = string.Format("{0}-{1}{2}.csv", "导出订单明细", DateTime.Now.ToString("yyyyMMdd"), GetRndNum(3));
//导出文件路径
string outPutFilePath = HttpContext.Current.Server.MapPath("Temp_Down/" + fileName);
//创建Excel对象
ExcelHelper excel = new ExcelHelper(reportModelPath, outPutFilePath);
SqlDataReader sdr = Get_Data();
while (sdr.Read())
{
ii++;
excel.SetCells(1 + ii, 1, ii);
excel.SetCells(1 + ii, 2, sdr["C_Name"]);
excel.SetCells(1 + ii, 3, sdr["C_Mtel"]);
excel.SetCells(1 + ii, 4, sdr["C_Tel"]);
excel.SetCells(1 + ii, 5, sdr["C_Province"]);
excel.SetCells(1 + ii, 6, sdr["C_Address"]);
excel.SetCells(1 + ii, 7, sdr["C_Postcode"]);
}
sdr.Close();
excel.SaveFile();
}
关于导出就简单写到这,另外下一节讲介绍如何通过这个类库上传Excel文件。 作者:WILLPAN热心网友 时间:2022-04-27 17:15
1.添加一个新的窗体default.aspx
2.拖入一个button控件
3.在解决方案管理器下面的当前项目点击鼠标右键.选择添加引用.在弹出的对话框选择com. 然后找到Microsoft Excel 11.0 Object Library(如果你电脑所装的是office的话.就是Microsoft Excel 11.0 Object Library) 选中后点击确定.
4.在default.aspx双击刚拖进的button控件.把default.aspx.cs页面的代码全部替换为以下.(注意文件名)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Excel = Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
string FilePath = Server.MapPath("1.xlsx");//这用到的文件名是1.xlsx 这的excel文件2007的
Excel.Application FileExcel = new Excel.ApplicationClass();
object Missing = System.Reflection.Missing.Value;
Excel.Workbook mybook = (Excel.Workbook)FileExcel.Workbooks.Open(FilePath, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing, Missing);
Excel.Sheets ExcelSheet1 = mybook.Worksheets;
Excel.Worksheet xlsheet = (Excel.Worksheet)ExcelSheet1.get_Item(1);
Excel.Range i = (Excel.Range)xlsheet.get_Range("B4", Missing);
i.ColumnWidth = 20;
i.Value2 = DateTime.Now.ToString();
FileExcel.DisplayAlerts = false;
mybook.Save();
mybook = null;
xlsheet = null;
FileExcel.Quit();
}
}热心网友 时间:2022-04-27 18:33
private static string sFile;
protected void Page_Load(object sender, EventArgs e)
{
sFile = Server.MapPath("2.xlsx");
}
protected void Button1_Click(object sender, EventArgs e)
{
Excel.Application oExcel = new Excel.ApplicationClass();
object objMissing = System.Reflection.Missing.Value;
Excel.Workbook mybook = (Excel.Workbook)oExcel.Workbooks.Open(sFile, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
Excel.Sheets excelSheets = mybook.Worksheets;
Excel.Worksheet xlsheet = (Excel.Worksheet)excelSheets.get_Item(1);
Excel.Range ex = (Excel.Range)xlsheet.get_Range("B4",objMissing);
ex.ColumnWidth = 20;
ex.Value2 = DateTime.Now.ToString();
oExcel.DisplayAlerts = false;
mybook.Save();
mybook = null;
xlsheet = null;
oExcel.Quit();
}
就这样了。测试成功。刚写的热心网友 时间:2022-04-27 20:07
在 <system.web>中添加<identity impersonate="true"/>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sFile = Server.MapPath("2006222102122.xls");
Excel.Application oExcel = new Excel.Application();
object objMissing = System.Reflection.Missing.Value;
Excel.Workbook mybook = (Excel.Workbook)oExcel.Workbooks.Open(sFile, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
Excel.Sheets excelSheets = mybook.Worksheets;
Excel.Worksheet xlsheet = (Excel.Worksheet)excelSheets.get_Item(1);
int i=1;
while (xlsheet.get_Range(xlsheet.Cells[i, 1], xlsheet.Cells[i, 1]).Text.ToString()!="")
{
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 1], xlsheet.Cells[i, 1]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 2], xlsheet.Cells[i, 2]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 3], xlsheet.Cells[i, 3]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 4], xlsheet.Cells[i, 4]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 5], xlsheet.Cells[i, 5]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 6], xlsheet.Cells[i, 6]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 7], xlsheet.Cells[i, 7]).Text.ToString() + "--");
Response.Write(xlsheet.get_Range(xlsheet.Cells[i, 8], xlsheet.Cells[i, 8]).Text.ToString() + "<br/>");
i++;
}
mybook = null;
xlsheet = null;
oExcel.Quit();
}
}热心网友 时间:2022-04-27 21:59
1楼是二蛋,瞎写的