public static string ExportAOrder(ExportData data)
{
var cellHeard = new Dictionary<string, string> {
{ "sort","序号"},
{ "modeName","型号名称"},
{ "modeCode","型号编码"},
{ "expectedQty","预定数量"},
{ "atualQty","已出库数量"},
};
string sheetName = "设备出库订单";
string fileName = sheetName + "-" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"; // 文件名称
string urlPath = "\\ExcelFiles\\" + fileName; // 文件下载的URL地址,供给前台下载
//Q8FilePath
string filePath = AppDomain.CurrentDomain.BaseDirectory + urlPath;
// 1.检测是否存在文件夹,若不存在就建立个文件夹
string directoryName = Path.GetDirectoryName(filePath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
HSSFWorkbook workbook = new HSSFWorkbook(); // 工作簿
ISheet sheet = workbook.CreateSheet(sheetName); // 工作表
//设置列数
int ColumnCount = 5;
sheet.SetColumnWidth(0, 13 * 256);
sheet.SetColumnWidth(1, 15 * 256);
sheet.SetColumnWidth(2, 15 * 256);
sheet.SetColumnWidth(3, 13 * 256);
sheet.SetColumnWidth(4, 15 * 256);
int rowindex = 0;
//标题
IRow rowTitle = sheet.CreateRow(rowindex);
rowTitle.Height = 40 * 20;
//在行中:建立单元格,参数为列号,从0计
ICell cell = rowTitle.CreateCell(0);
//设置单元格内容
cell.SetCellValue(sheetName);
ICellStyle style = workbook.CreateCellStyle();
//设置单元格的样式:水平对齐居中
style.Alignment = HorizontalAlignment.Center;
style.VerticalAlignment = VerticalAlignment.Center;
//新建一个字体样式对象
IFont font = workbook.CreateFont();
//设置字体加粗样式
//font.Boldweight = short.MaxValue;
font.IsBold = true;
font.FontHeightInPoints = 25;
//使用SetFont方法将字体样式添加到单元格样式中
style.SetFont(font);
//将新的样式赋给单元格
cell.CellStyle = style;
sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, ColumnCount - 1));
rowindex += 3;
IRow mainRow1 = sheet.CreateRow(rowindex);
mainRow1.CreateCell(0).SetCellValue("单号:");
mainRow1.CreateCell(1).SetCellValue(data.orderId);
rowindex++;
IRow mainRow2 = sheet.CreateRow(rowindex);
mainRow2.CreateCell(0).SetCellValue("客户编号:");
mainRow2.CreateCell(1).SetCellValue(data.comNumber);
mainRow2.CreateCell(3).SetCellValue("销售姓名:");
mainRow2.CreateCell(4).SetCellValue(data.saleName);
rowindex++;
IRow mainRow3 = sheet.CreateRow(rowindex);
mainRow3.CreateCell(0).SetCellValue("客户名称:");
mainRow3.CreateCell(1).SetCellValue(data.comName);
mainRow3.CreateCell(3).SetCellValue("联系人:");
mainRow3.CreateCell(4).SetCellValue(data.agentName);
rowindex++;
IRow mainRow4 = sheet.CreateRow(rowindex);
mainRow4.CreateCell(0).SetCellValue("客户电话:");
mainRow4.CreateCell(1).SetCellValue(data.comPhone);
mainRow4.CreateCell(3).SetCellValue("联系号码:");
mainRow4.CreateCell(4).SetCellValue(data.agentPhone);
rowindex++;
IRow mainRow5 = sheet.CreateRow(rowindex);
mainRow5.CreateCell(0).SetCellValue("客户地址:");
mainRow5.CreateCell(1).SetCellValue(data.comAdress);
mainRow5.CreateCell(3).SetCellValue("联系人地址::");
mainRow5.CreateCell(4).SetCellValue(data.agentAdress);
IRow row = sheet.CreateRow(rowindex);
List<string> keys = new List<string>();
foreach (var item in cellHeard.Keys)
{
keys.Add(item);
}
for (int i = 0; i < keys.Count; i++)
{
ICell FiledTitle = row.CreateCell(i);
ICellStyle FiledStyle = workbook.CreateCellStyle();
FiledTitle.SetCellValue(cellHeard[keys[i]]); // 列名为Key的值
FiledStyle.Alignment = HorizontalAlignment.Center;
//新建一个字体样式对象
IFont Filedfont = workbook.CreateFont();
//设置字体加粗样式
Filedfont.Boldweight = short.MaxValue;
//使用SetFont方法将字体样式添加到单元格样式中
FiledStyle.SetFont(Filedfont);
FiledStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.CornflowerBlue.Index;
FiledStyle.FillPattern = FillPattern.SolidForeground;
FiledTitle.CellStyle = FiledStyle;
}
rowindex++;
foreach (var item in data.list)
{
IRow rowTmp = sheet.CreateRow(rowindex);
int cellIndex = 0;
foreach (var key in keys)
{
string cellValue = ""; // 单元格的值
object properotyValue = null; // 属性的值
var property = item.GetType().GetProperty(key);
if (property.IsNotNull())
{
properotyValue = property.GetValue(item, null);
}
if (properotyValue.IsNotNull())
{
cellValue = properotyValue.ToString();
}
rowTmp.CreateCell(cellIndex).SetCellValue(cellValue);
cellIndex++;
}
rowindex++;
}
using (var file = new FileStream(filePath, FileMode.Create))
{
workbook.Write(file);
file.Close();
}
return filePath;
}
public class ExportData
{
public ExportData()
{
list = new List<RowData>();
}
public string orderId { get; set; }
public string comNumber { get; set; }
public string comName { get; set; }
public string comPhone { get; set; }
public string comAdress { get; set; }
public string saleName { get; set; }
public string agentName { get; set; }
public string agentPhone { get; set; }
public string agentAdress { get; set; }
public List<RowData> list { get; set; }
}
public class RowData
{
public int sort { get; set; }
public string modeCode { get; set; }
public string modeName { get; set; }
public int expectedQty { get; set; }
public int atualQty { get; set; }
}