Yah*_*hia 12
MS提供OpenXML SDK V 2.5 - 请参阅https://msdn.microsoft.com/en-us/library/bb448854(v=office.15).aspx
这可以读取+写入MS Office文件(包括Excel)...
另一个选项见http://www.codeproject.com/KB/office/OpenXML.aspx
如果您需要更多像渲染,公式等,那么有不同的商业库,如Aspose和Flexcel ......
您是否听说过 NPOI,这是一个无需安装 Microsoft Office 即可读取/写入 Office 格式的 .NET 库。没有 COM+,就没有互操作。GitHub页面
\n\n这是我的 Excel 导出课程
\n\n/*\n * User: TMPCSigit aswzen@gmail.com\n * Date: 25/11/2019\n * Time: 11:28\n * \n */\nusing System;\nusing System.IO;\nusing System.Text.RegularExpressions;\nusing System.Windows.Forms;\n\nusing NPOI.HSSF.UserModel;\nusing NPOI.SS.UserModel;\nusing NPOI.XSSF.UserModel;\n\nnamespace Employee_Manager\n{\n public static class ExportHelper\n { \n public static void WriteCell( ISheet sheet, int columnIndex, int rowIndex, string value )\n {\n var row = sheet.GetRow( rowIndex ) ?? sheet.CreateRow( rowIndex );\n var cell = row.GetCell( columnIndex ) ?? row.CreateCell( columnIndex );\n\n cell.SetCellValue( value );\n }\n public static void WriteCell( ISheet sheet, int columnIndex, int rowIndex, double value )\n {\n var row = sheet.GetRow( rowIndex ) ?? sheet.CreateRow( rowIndex );\n var cell = row.GetCell( columnIndex ) ?? row.CreateCell( columnIndex );\n\n cell.SetCellValue( value );\n }\n public static void WriteCell( ISheet sheet, int columnIndex, int rowIndex, DateTime value )\n {\n var row = sheet.GetRow( rowIndex ) ?? sheet.CreateRow( rowIndex );\n var cell = row.GetCell( columnIndex ) ?? row.CreateCell( columnIndex );\n\n cell.SetCellValue( value );\n }\n public static void WriteStyle( ISheet sheet, int columnIndex, int rowIndex, ICellStyle style )\n {\n var row = sheet.GetRow( rowIndex ) ?? sheet.CreateRow( rowIndex );\n var cell = row.GetCell( columnIndex ) ?? row.CreateCell( columnIndex );\n\n cell.CellStyle = style;\n }\n\n public static IWorkbook CreateNewBook( string filePath )\n {\n IWorkbook book;\n var extension = Path.GetExtension( filePath );\n\n // HSSF => Microsoft Excel(xls\xe5\xbd\xa2\xe5\xbc\x8f)(excel 97-2003)\n // XSSF => Office Open XML Workbook\xe5\xbd\xa2\xe5\xbc\x8f(xlsx\xe5\xbd\xa2\xe5\xbc\x8f)(excel 2007\xe4\xbb\xa5\xe9\x99\x8d)\n if( extension == ".xls" ) {\n book = new HSSFWorkbook();\n }\n else if( extension == ".xlsx" ) {\n book = new XSSFWorkbook();\n }\n else {\n throw new ApplicationException( "CreateNewBook: invalid extension" );\n }\n\n return book;\n }\n public static void createXls(DataGridView dg){\n try {\n string filePath = "";\n SaveFileDialog sfd = new SaveFileDialog();\n sfd.Filter = "Excel XLS (*.xls)|*.xls";\n sfd.FileName = "Export.xls";\n if (sfd.ShowDialog() == DialogResult.OK)\n {\n filePath = sfd.FileName;\n var book = CreateNewBook( filePath );\n book.CreateSheet( "Employee" );\n var sheet = book.GetSheet( "Employee" );\n int columnCount = dg.ColumnCount;\n string columnNames = "";\n string[] output = new string[dg.RowCount + 1];\n for (int i = 0; i < columnCount; i++)\n {\n WriteCell( sheet, i, 0, SplitCamelCase(dg.Columns[i].Name.ToString()) );\n }\n for (int i = 0; i < dg.RowCount; i++)\n {\n for (int j = 0; j < columnCount; j++)\n {\n var celData = dg.Rows[i].Cells[j].Value;\n if(celData == "" || celData == null){\n celData = "-";\n }\n if(celData.ToString() == "System.Drawing.Bitmap"){\n celData = "Ada";\n }\n WriteCell( sheet, j, i+1, celData.ToString() );\n }\n }\n var style = book.CreateCellStyle();\n style.DataFormat = book.CreateDataFormat().GetFormat( "yyyy/mm/dd" );\n WriteStyle( sheet, 0, 4, style );\n using( var fs = new FileStream( filePath, FileMode.Create ) ) {\n book.Write( fs );\n }\n }\n }\n catch( Exception ex ) {\n Console.WriteLine( ex );\n }\n }\n public static string SplitCamelCase(string input)\n {\n return Regex.Replace(input, "(?<=[a-z])([A-Z])", " $1", RegexOptions.Compiled);\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
private void button1_Click(object sender, EventArgs e)
{
Excel.Application xlApp ;
Excel.Workbook xlWorkBook ;
Excel.Worksheet xlWorkSheet ;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkBook.SaveAs("csharp-Excel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\\csharp-Excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码直接取自csharp.net,请浏览网站.