using System; using System.IO; using System.Data; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.XSSF.UserModel; namespace cfx.office { public class tExcelWriter { // 其它代码 // 写入已存在的文件 public bool WriteExisted() { string ext = Path.GetExtension(myFileName).ToLower(); if (ext == ".xls") return WriteXlsExisted(); else return WriteXlsxExisted(); } // 写入已存在的xls文件 private bool WriteXlsExisted() { try { if (myData == null || myData.Columns.Count < 1 || File.Exists(myFileName) == false) return false; // using (FileStream fs = new FileStream(myFileName, FileMode.Open, FileAccess.Read)) { using (HSSFWorkbook wb = new HSSFWorkbook(fs)) { int sheetIndex = 0; if (mySheetName != null && mySheetName.Trim().Length > 0) sheetIndex = wb.GetSheetIndex(mySheetName); if (sheetIndex == -1) sheetIndex = 0; ISheet sheet = wb.GetSheetAt(sheetIndex); // int curRow = myBeginRow; // 写入列名 if (myWriteColumnName) { IRow sheetRow = sheet.GetRow(curRow); if (sheetRow == null) sheetRow = sheet.CreateRow(curRow); for (int col = 0; col < myData.Columns.Count; col++) { int colIndex = col + myBeginColumn; ICell cell = sheetRow.GetCell(colIndex); if (cell == null) cell = sheetRow.CreateCell(colIndex); cell.SetCellType(CellType.String); cell.SetCellValue(myData.Columns[col].ColumnName); } curRow++; } // 日期和时间格式 IDataFormat dataFormat = wb.CreateDataFormat(); ICellStyle datetimeStyle = wb.CreateCellStyle(); datetimeStyle.DataFormat = dataFormat.GetFormat("yyyy/MM/dd HH:mm:ss"); // 写入数据行 for (int row = 0; row < myData.Rows.Count; row++) { IRow sheetRow = sheet.GetRow(curRow); if (sheetRow == null) sheetRow = sheet.CreateRow(curRow); for (int col = 0; col < myData.Columns.Count; col++) { int colIndex = col + myBeginColumn; ICell cell = sheetRow.GetCell(colIndex); if (cell == null) cell = sheetRow.CreateCell(colIndex); if (myData.Rows[row][col] == DBNull.Value) { cell.SetCellValue(""); } else { Type dataType = myData.Columns[col].DataType; if (Comm.IsNumeric(dataType)) { cell.SetCellType(CellType.Numeric); double result; if (Obj.TryToDbl(myData.Rows[row][col], out result)) cell.SetCellValue(result); } else if (dataType.Name == "DateTime") { cell.CellStyle = datetimeStyle; DateTime result; if (Obj.TryToDate(myData.Rows[row][col], out result)) cell.SetCellValue(result); } else if (dataType.Name == "Boolean") { cell.SetCellType(CellType.Boolean); cell.SetCellValue(Obj.ToBool(myData.Rows[row][col])); } else { cell.SetCellType(CellType.String); cell.SetCellValue(Obj.ToStr(myData.Rows[row][col])); } } } curRow++; } // using (FileStream fs1 = new FileStream(myFileName, FileMode.Create, FileAccess.Write)) { wb.Write(fs1); } } return true; } } catch { return false; } } // 写入已存在的xls文件 private bool WriteXlsxExisted() { try { if (myData == null || myData.Columns.Count < 1 || File.Exists(myFileName) == false) return false; // using (FileStream fs = new FileStream(myFileName, FileMode.Open, FileAccess.Read)) { using (XSSFWorkbook wb = new XSSFWorkbook(fs)) { int sheetIndex = 0; if (mySheetName != null && mySheetName.Trim().Length > 0) sheetIndex = wb.GetSheetIndex(mySheetName); if (sheetIndex == -1) sheetIndex = 0; ISheet sheet = wb.GetSheetAt(sheetIndex); // int curRow = myBeginRow; // 写入列名 if (myWriteColumnName) { IRow sheetRow = sheet.GetRow(curRow); if (sheetRow == null) sheetRow = sheet.CreateRow(curRow); for (int col = 0; col < myData.Columns.Count; col++) { int colIndex = col + myBeginColumn; ICell cell = sheetRow.GetCell(colIndex); if (cell == null) cell = sheetRow.CreateCell(colIndex); cell.SetCellType(CellType.String); cell.SetCellValue(myData.Columns[col].ColumnName); } curRow++; } // 日期和时间格式 IDataFormat dataFormat = wb.CreateDataFormat(); ICellStyle datetimeStyle = wb.CreateCellStyle(); datetimeStyle.DataFormat = dataFormat.GetFormat("yyyy/MM/dd HH:mm:ss"); // 写入数据行 for (int row = 0; row < myData.Rows.Count; row++) { IRow sheetRow = sheet.GetRow(curRow); if (sheetRow == null) sheetRow = sheet.CreateRow(curRow); for (int col = 0; col < myData.Columns.Count; col++) { int colIndex = col + myBeginColumn; ICell cell = sheetRow.GetCell(colIndex); if (cell == null) cell = sheetRow.CreateCell(colIndex); if (myData.Rows[row][col] == DBNull.Value) { cell.SetCellValue(""); } else { Type dataType = myData.Columns[col].DataType; if (Comm.IsNumeric(dataType)) { cell.SetCellType(CellType.Numeric); double result; if (Obj.TryToDbl(myData.Rows[row][col], out result)) cell.SetCellValue(result); } else if (dataType.Name == "DateTime") { cell.CellStyle = datetimeStyle; DateTime result; if (Obj.TryToDate(myData.Rows[row][col], out result)) cell.SetCellValue(result); } else if (dataType.Name == "Boolean") { cell.SetCellType(CellType.Boolean); cell.SetCellValue(Obj.ToBool(myData.Rows[row][col])); } else { cell.SetCellType(CellType.String); cell.SetCellValue(Obj.ToStr(myData.Rows[row][col])); } } } curRow++; } // using (FileStream fs1 = new FileStream(myFileName, FileMode.Create, FileAccess.Write)) { wb.Write(fs1); } } return true; } } catch { return false; } } } } |