在尝试从网络上的点点滴滴(包括StackOverFlow)构建我自己的类之后,我找到了上面提到的库,并且片刻之后还有一个功能齐全的Excel文件 .

我已将下面的尝试粘贴到任何感觉完成它的冲动的人的启发下 . 它部分完成,并且日期和字符串单元格创建存在问题 .

在尝试使用此类之前,首先下载closedXML并首先尝试 .

考虑自己警告 .

///

/// This class allows for the easy creation of a simple Excel document who's sole purpose is to contain some export data.

/// The document is created using OpenXML.

///

internal class SimpleExcelDocument : IDisposable

{

SheetData sheetData;

///

/// Constructor is nothing special because the work is done at export.

///

internal SimpleExcelDocument()

{

sheetData = new SheetData();

}

#region Get Cell Reference

public Cell GetCell(string fullAddress)

{

return sheetData.Descendants().Where(c => c.CellReference == fullAddress).FirstOrDefault();

}

public Cell GetCell(uint rowId, uint columnId, bool autoCreate)

{

return GetCell(getColumnName(columnId), rowId, autoCreate);

}

public Cell GetCell(string columnName, uint rowId, bool autoCreate)

{

return getCell(sheetData, columnName, rowId, autoCreate);

}

#endregion

#region Get Cell Contents

// See: http://msdn.microsoft.com/en-us/library/ff921204.aspx

//

#endregion

#region Set Cell Contents

public void SetValue(uint rowId, uint columnId, bool value)

{

Cell cell = GetCell(rowId, columnId, true);

cell.DataType = CellValues.Boolean;

cell.CellValue = new CellValue(BooleanValue.FromBoolean(value));

}

public void SetValue(uint rowId, uint columnId, double value)

{

Cell cell = GetCell(rowId, columnId, true);

cell.DataType = CellValues.Number;

cell.CellValue = new CellValue(DoubleValue.FromDouble(value));

}

public void SetValue(uint rowId, uint columnId, Int64 value)

{

Cell cell = GetCell(rowId, columnId, true);

cell.DataType = CellValues.Number;

cell.CellValue = new CellValue(IntegerValue.FromInt64(value));

}

public void SetValue(uint rowId, uint columnId, DateTime value)

{

Cell cell = GetCell(rowId, columnId, true);

//cell.DataType = CellValues.Date;

cell.CellValue = new CellValue(value.ToOADate().ToString());

cell.StyleIndex = 1;

}

public void SetValue(uint rowId, uint columnId, string value)

{

Cell cell = GetCell(rowId, columnId, true);

cell.InlineString = new InlineString(value.ToString());

cell.DataType = CellValues.InlineString;

}

public void SetValue(uint rowId, uint columnId, object value)

{

bool boolResult;

Int64 intResult;

DateTime dateResult;

Double doubleResult;

string stringResult = value.ToString();

if (bool.TryParse(stringResult, out boolResult))

{

SetValue(rowId, columnId, boolResult);

}

else if (DateTime.TryParse(stringResult, out dateResult))

{

SetValue(rowId, columnId,dateResult);

}

else if (Int64.TryParse(stringResult, out intResult))

{

SetValue(rowId, columnId, intResult);

}

else if (Double.TryParse(stringResult, out doubleResult))

{

SetValue(rowId, columnId, doubleResult);

}

else

{

// Just assume that it is a plain string.

SetValue(rowId, columnId, stringResult);

}

}

#endregion

public SheetData ExportAsSheetData()

{

return sheetData;

}

public void ExportAsXLSXStream(Stream outputStream)

{

// See: http://blogs.msdn.com/b/chrisquon/archive/2009/07/22/creating-an-excel-spreadsheet-from-scratch-using-openxml.aspx for some ideas...

// See: http://stackoverflow.com/questions/1271520/opening-xlsx-in-office-2003

using (SpreadsheetDocument package = SpreadsheetDocument.Create(outputStream, SpreadsheetDocumentType.Workbook))

{

// Setup the basics of a spreadsheet document.

package.AddWorkbookPart();

package.WorkbookPart.Workbook = new Workbook();

WorksheetPart workSheetPart = package.WorkbookPart.AddNewPart();

workSheetPart.Worksheet = new Worksheet(sheetData);

workSheetPart.Worksheet.Save();

// create the worksheet to workbook relation

package.WorkbookPart.Workbook.AppendChild(new Sheets());

Sheet sheet = new Sheet {

Id = package.WorkbookPart.GetIdOfPart(workSheetPart),

SheetId = 1,

Name = "Sheet 1"

};

package.WorkbookPart.Workbook.GetFirstChild().AppendChild(sheet);

package.WorkbookPart.Workbook.Save();

package.Close();

}

}

#region Internal Methods

private static string getColumnName(uint columnId)

{

if (columnId < 1)

{

throw new Exception("The column # can't be less then 1.");

}

columnId--;

if (columnId >= 0 && columnId < 26)

return ((char)('A' + columnId)).ToString();

else if (columnId > 25)

return getColumnName(columnId / 26) + getColumnName(columnId % 26 + 1);

else

throw new Exception("Invalid Column #" + (columnId + 1).ToString());

}

// Given a worksheet, a column name, and a row index,

// gets the cell at the specified column

private static Cell getCell(SheetData worksheet,

string columnName, uint rowIndex, bool autoCreate)

{

Row row = getRow(worksheet, rowIndex, autoCreate);

if (row == null)

return null;

Cell foundCell = row.Elements().Where(c => string.Compare

(c.CellReference.Value, columnName +

rowIndex, true) == 0).FirstOrDefault();

if (foundCell == null && autoCreate)

{

foundCell = new Cell();

foundCell.CellReference = columnName;

row.AppendChild(foundCell);

}

return foundCell;

}

// Given a worksheet and a row index, return the row.

// See: http://msdn.microsoft.com/en-us/library/bb508943(v=office.12).aspx#Y2142

private static Row getRow(SheetData worksheet, uint rowIndex, bool autoCreate)

{

if (rowIndex < 1)

{

throw new Exception("The row # can't be less then 1.");

}

Row foundRow = worksheet.Elements().Where(r => r.RowIndex == rowIndex).FirstOrDefault();

if (foundRow == null && autoCreate)

{

foundRow = new Row();

foundRow.RowIndex = rowIndex;

worksheet.AppendChild(foundRow);

}

return foundRow;

}

#endregion

#region IDisposable Stuff

private bool _disposed;

//private bool _transactionComplete;

///

/// This will dispose of any open resources.

///

public void Dispose()

{

Dispose(true);

// Use SupressFinalize in case a subclass

// of this type implements a finalizer.

GC.SuppressFinalize(this);

}

protected virtual void Dispose(bool disposing)

{

// If you need thread safety, use a lock around these

// operations, as well as in your methods that use the resource.

if (!_disposed)

{

if (disposing)

{

//if (!_transactionComplete)

// Commit();

}

// Indicate that the instance has been disposed.

//_transaction = null;

_disposed = true;

}

}

#endregion

}

java openxml_Excel单元格中的OpenXml和Date格式相关推荐

  1. 导出excel此单元格中的数字为文本格式,或者其前面有撇号

    前言: 无论大家使用的是java,js,.net等技术导出excel,都会出现下面的问题. 这篇文章只提供解决思路,以及用easyexcel导出 的处理方式,其他技术大家可以举一反三. 1.导出exc ...

  2. Java 在Excel单元格中应用一种/多种字体样式

    在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本 ...

  3. java excel 字体_Java 在Excel单元格中应用一种/多种字体样式

    在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本 ...

  4. java字体美化_Java 在Excel单元格中应用一种/多种字体样式(实例代码)

    这篇文章主要介绍了Java 在Excel单元格中应用一种/多种字体样式,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下 在Excel表格中,设置单元格字体样式时,可 ...

  5. JAVA导出excel表格标题加粗,【excle特定文字加粗】java如何设置excel单元格中部分文字加粗 例如:标题(加粗): 内容(不加粗)...

    java如何设置excel单元格中部分文字加粗 例如:标题(加粗): 内容(不加粗) HSSFCellStyle style = (HSSFCellStyle) wb.createCellStyle( ...

  6. java 单元格内容加粗,excle单元格如何加粗《EXCEL中怎么对单元格中,公式的部分字段加粗?》...

    问下EXCEL表格分割线怎么加粗 单元格样式设置包含分割线加粗. Excel表格单元格线样式操作如下: ①框选单元格--右击--设置单元格 ②弹出"设置单元格式"--边框--根据实 ...

  7. java excel 字体_Java 在Excel单元格中应用一种/多种字体样式(实例代码)

    在Excel表格中,设置单元格字体样式时,可以对单元格内的所有字符应用同一样式,即获取指定单元,应用样式即可:另外也可以对单元格内的不同字符内容应用不同字体样式,即获取单元格中的字符位置,应用样式:本 ...

  8. java excel列宽自动换行_Excel单元格中数据如何自动换行以便适应单元格的大小

    Excel单元格中数据如何自动换行以便适应单元格的大小 时间:2014-04-26   作者:snow   来源:互联网 在处理数据的过程中难免会遇到一些问题,而我们并不可能在第一时间内解决,就比如在 ...

  9. JAVA通过poi实现excel表格制作并且将图片放入到指定的单元格中(可以循环插入)

    废话不多说,直接上代码,上效果图 @RestController @Api(tags = "报表") @RequestMapping("/export") @C ...

  10. java 向word中添加excel附件并向excel单元格中加入图片并压缩图片并根据图片动态控制单元格高度宽度

    word中加入excel附件 excel单元格中插入图片 word freemarker Excel poi 制作, jxl 插入图片 压缩图片/宽高,动态控制单元格高度与宽度 1.word 需要模板 ...

最新文章

  1. 送你38个常用的Python库,数值计算、可视化、机器学习等8大领域都有了
  2. 【C语言】简单C编程题-同位相同的N项之和/标准输入花括号成对判断/行号行输出...
  3. Java 性能优化实战记录(3)--JVM OOM的分析和原因追查
  4. 怎么在服务器中修改sql权限设置密码,SQL server数据库的权限设置
  5. 《团队——科学计算器代码设计规范》
  6. vue复选框组件自定义对勾_vue+element:树级复选框组件使用
  7. druid seata 配置_架构设计 | 基于Seata中间件,微服务模式下事务管理
  8. sql中count(1)、count(*)和count(字段名)的区别
  9. 第十一届蓝桥杯省赛C++组试题 第6题
  10. 2011 MVP大奖礼品包,那是相当的给力啊!!
  11. 超强领先!Transformer图像复原效果显著!
  12. day18 java的数组
  13. python中列表的查_每日一记----python中的列表【查询和插入】
  14. linux环境下编译llvm源码
  15. mysql数据库最多列_mysql多列索引和最左前缀
  16. 【git】结合Gerrit 代码审查工具的操作流程,工作流程
  17. solaris 命令大全
  18. 佛,我心中的一朵莲花
  19. AtCoder Beginner Contest 172 E - NEQ(二项式反演)
  20. 2019 ICPC 南京 F题 Paper Grading

热门文章

  1. springboot 联合查询
  2. s7edge固件android7.0,欧版S7 edge刷上Android 7.0之后:超级流畅
  3. 关闭445端口即关闭共享文件功能
  4. 目标检测-RCNN系列
  5. 一个B站下载视频的网站
  6. 001云E办项目之创建项目
  7. crmeb多商户二开crmeb架构二开文档异常处理【4】
  8. 【Code】numpy、pytorch实现全连接神经网络
  9. US Domain Center 建站神器
  10. vue框架对接手机app