直接开整,使用:
//读取excel
var FileStreamfile = new FileStream(Server.MapPath("~/PrintTemplet/LuXianTemplet.xls"), FileMode.Open, FileAccess.Read);
HSSFWorkbook hssfworkbook = new HSSFWorkbook(FileStreamfile);
HSSFSheet sheet1 = (HSSFSheet)hssfworkbook.GetSheet("Sheet1");
//设置不是合并的单元格的内容
sheet1.GetRow(1).GetCell(0).SetCellValue(1111);
//设置合并单元格的内容
/// <summary>
/// 设置合并单元格的值
/// </summary>
/// <param name="sheet">第一个sheet</param>
/// <param name="row">行</param>
/// <param name="column">列</param>
/// <param name="setvalue">设置的内容</param>
/// <returns></returns>
private static void SetMergedRegionStr(ISheet sheet, int row, int column,string setvalue)
{
//初始化坐标
Point start = new Point(0, 0);
Point end = new Point(0, 0);
//获取合并单元格的数量
int regionsCount = sheet.NumMergedRegions;
//初始化单元格的值
object cellValue = new object();
//判断坐标
for (int j = 0; j < regionsCount; j++)
{
//获得不同坐标
CellRangeAddress range = sheet.GetMergedRegion(j);
//判读参数坐标是否存在
if (row >= range.FirstRow && row <= range.LastRow && column >= range.FirstColumn && column <= range.LastColumn)
{
//获取坐标
start = new Point(range.FirstRow, range.FirstColumn);
end = new Point(range.LastRow, range.LastColumn);
//坐标x=row,y=colum
int x = start.X;//获取所在坐标行号
int y = start.Y;//获取所在坐标列号
//获取所在行
IRow fRow = sheet.GetRow(x);
//获取所在列
ICell fCell = fRow.GetCell(y);
//判断cell的类型
fCell.SetCellValue(setvalue);
}
}
}
//excel转html
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
// 设置输出参数
hssfworkbook.SetSheetName(0," ");//隐藏表名
excelToHtmlConverter.OutputColumnHeaders = false;
excelToHtmlConverter.OutputRowNumbers = false;
excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
// 处理的Excel文件
excelToHtmlConverter.ProcessWorkbook(hssfworkbook);
excelToHtmlConverter.Document.InnerXml =
excelToHtmlConverter.Document.InnerXml.Insert(
excelToHtmlConverter.Document.InnerXml.IndexOf("<head>", 0) + 6,
@"<style>table{width:100% } </style>"
);
excelToHtmlConverter.Document.Save(Server.MapPath("~/PrintTemplet/" + FileName));
创建
//建立空白工作薄
IWorkbook workbook = new HSSFWorkbook();
//在工作薄中建立工作表
ISheet sheet = workbook.CreateSheet();
//填充筛选的内容
sheet.CreateRow(0).CreateCell(0).SetCellValue("省份");
sheet.CreateRow(1).CreateCell(0).SetCellValue("河北省");
sheet.CreateRow(2).CreateCell(0).SetCellValue("湖南省");
//验证数字格式问题
sheet.GetRow(1).CreateCell(2).SetCellValue("123");
sheet.GetRow(2).CreateCell(2).SetCellValue(123);
//设置Excel的自动筛选
CellRangeAddress c = CellRangeAddress.ValueOf("A1");
sheet.SetAutoFilter(c);
//写文件
using (FileStream fs = new FileStream("haha.xls", FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
}