// 生成excel表格 ///
///
publicvoidOutPwd(System.Data.DataTabledtData, stringfileName, stringgetDate) { try
{
stringfilePath = AppDomain.CurrentDomain.BaseDirectory + @\ + fileName + \;
stringmodelPath = AppDomain.CurrentDomain.BaseDirectory + @\WCModel.xls\;
#region NPOI 导出Excel
FileStreamfile = newFileStream(modelPath, FileMode.Open, FileAccess.Read);
HSSFWorkbookhssfworkbook = newHSSFWorkbook(file); HSSFSheetsheet1 = hssfworkbook.GetSheet(\); HSSFRowdr1 = sheet1.CreateRow(0);
HSSFCellcell = dr1.CreateCell(0);
string[] ymd = getDate.Split('-');
cell.SetCellValue(ymd[0] + \年\ + ymd[1] + \月\ + ymd[2] + \日快消通进销存分析表\);
HSSFCellStylestyle = hssfworkbook.CreateCellStyle(); style.Alignment = HSSFCellStyle.ALIGN_CENTER; HSSFFontfont = hssfworkbook.CreateFont(); font.FontHeightInPoints = 12; font.Color = HSSFColor.BLACK.index; font.FontHeight = 20 * 20;
font.Boldweight = HSSFFont.BOLDWEIGHT_BOLD; style.SetFont(font); cell.CellStyle = style;
for (inti = 0; iHSSFRowdr = sheet1.CreateRow(i + 4); for (intj = 0; jdr.CreateCell(j).SetCellValue(dtData.Rows[i][j].ToString())
;
} }
sheet1.ForceFormulaRecalculation = true; //合并
for (intm = 0; m< 2; m++) {
for (intn = 4; nif (sheet1.GetRow(n).GetCell(m).ToString() == sheet1.GetRow(n + 1).GetCell(m).ToString()
&& !string.IsNullOrEmpty(sheet1.GetRow(n).GetCell(m).ToString()))
{
sheet1.AddMergedRegion(newRegion(n, m, n + 1, m)); }
elseif (sheet1.GetRow(n).GetCell(m).ToString() == sheet1.GetRow(n).GetCell(m + 1).ToString()) {
sheet1.AddMergedRegion(newRegion(n, m, n, m + 1)); } }
}
sheet1.AddMergedRegion(newRegion(dtData.Rows.Count + 3, 0, dtData.Rows.Count + 3, 3));
//sheet1.AddMergedRegion(new Region(0, 0, 0, 5)); file = newFileStream(filePath, FileMode.Create); hssfworkbook.Write(file); file.Close();
if (!string.IsNullOrEmpty(fileUpPath)) {
stringupPath = AppDomain.CurrentDomain.BaseDirectory + @\ + fileUpPath + \;
if (System.IO.File.Exists(upPath)) {
System.IO.File.Delete(upPath); //直接删除其中的文件 } }
#endregion } catch (Exceptionex) {
thrownewException(ex.Message);
} }