//导入数据
protected void btnImport_Click(object sender, EventArgs e)
{
//判断fileImport控件中是否有需要上传的文件
if (this.fileImport.HasFile)
{
//判断文件类型是否符合要求
if (Path.GetExtension(this.fileImport.FileName) != ".xls")
{
MessageTips("上传的文件类型不符合要求", MessageTipsType.Error);
return;
}
DataTable dt = GetImportInfo();//将Excel中的文件读取到dt中
if (dt != null && dt.Rows.Count > 0)
{
BOBasicInfoBusiness.InsertASIList(dt);//上传文件
MessageTips("上传成功", MessageTipsType.Success);
}
else
{
MessageTips("上传失败", MessageTipsType.Error);
}
}
else
{
MessageTips("请选择导入模板", MessageTipsType.Error);
}
}
//导出数据
protected void btnModel_Click(object sender, EventArgs e)
{
HSSFWorkbook hssfworkbook = new HSSFWorkbook();
HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.CreateSheet("SI编码维护列表");
//蓝色
HSSFCellStyle hssBlue = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
hssBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GOLD.index;
hssBlue.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.GOLD.index;
hssBlue.FillPattern = FillPatternType.SQUARES;
//边框
hssBlue.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
hssBlue.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
hssBlue.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
hssBlue.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
HSSFFont font = (HSSFFont)hssfworkbook.CreateFont();
font.Boldweight = 600;
font.FontHeight = 200;
hssBlue.SetFont((HSSFFont)font);
HSSFRow row = (HSSFRow)sheet1.CreateRow(0);
ICell cell0 = row.CreateCell(0);
cell0.SetCellValue("SI编码维护列表");
cell0.CellStyle = (ICellStyle)hssBlue; row.Height = 300;
HSSFRow row1 = (HSSFRow)sheet1.CreateRow(1);
ICell cell1 = row1.CreateCell(0);
cell1.SetCellValue("SI编码");
ICell cell2 = row1.CreateCell(1);
cell2.SetCellValue("SI名称");
//添加格式(数字的字符串格式)
ICellStyle cellStyle = hssfworkbook.CreateCellStyle();
IDataFormat format = hssfworkbook.CreateDataFormat();
cellStyle.DataFormat = format.GetFormat("@");
sheet1.SetDefaultColumnStyle(0, cellStyle);
sheet1.SetDefaultColumnStyle(1, cellStyle);
//合并单元格(起始行,结束行,起始列,结束列)
sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 1));
//设置列宽
sheet1.SetColumnWidth(0, 5000); sheet1.SetColumnWidth(1, 10000);
//填充信息(将数据库中的数据填入dt中)
DataTable dt = BOBasicInfoBusiness.GetASIList();
if (dt != null || dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
HSSFRow rowtemp = (HSSFRow)sheet1.CreateRow(i + 2);
ICell celltemp1 = rowtemp.CreateCell(0);
celltemp1.SetCellValue(dt.Rows[i]["SICode"].ToString());
celltemp1.SetCellType(CellType.STRING);
ICell celltemp2 = rowtemp.CreateCell(1);
celltemp2.SetCellValue(dt.Rows[i]["SIName"].ToString());
celltemp2.SetCellType(CellType.STRING);
}
}
//保存修改的模板
string savePath = this.MapPath("~/BaseInfo/Temp/SICodeList.xls");
using (FileStream file = new FileStream(savePath, FileMode.Create))
{
hssfworkbook.Write(file);//写入数据至BaseInfo/Temp/SICodeList.xls
} string path = this.MapPath("~/BaseInfo/Temp/SICodeList.xls");
ExcelOutE(this, path);//打开或保存此路径下的文件
}
//将Excel中的文件读取到dt中
private DataTable GetImportInfo()
{
DataTable dt = null;
HSSFWorkbook workbook = new HSSFWorkbook(this.fileImport.FileContent);
HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
HSSFRow contentRow = (HSSFRow)sheet.GetRow(2);
dt = new DataTable();
dt.Columns.Add("SICode");
for (int i = (sheet.FirstRowNum + 2); i < sheet.LastRowNum + 1; i++)
{
HSSFRow row = (HSSFRow)sheet.GetRow(i);
//取得每一行的每一列的值
string SICode = row.GetCell(0).ToString();
string SIName = row.GetCell(1).ToString();
//插入DataTable dt.Rows.Add(new string[] { SICode,SIName});
}
workbook = null;
sheet = null;
return dt;
}
//打开或保存此路径下的文件
private void ExcelOutE(Page Page, string fileName)
{
HttpResponse Response = Page.Response;
HttpServerUtility Server = Page.Server;
string fn = Server.UrlDecode(fileName);
FileStream fileStream = new FileStream(fn, FileMode.Open);
long fileSize = fileStream.Length; fileStream.Close();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=Sheet1.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
Response.AddHeader("Content-Length", fileSize.ToString());
Page.EnableViewState = false;
Response.WriteFile(fn);
Response.Flush();
Response.End();
}
原文:http://www.cnblogs.com/lijie20111015/p/3893040.html