我之前成功地将Excel转换为数据表,但是现在,我有格式化的表格,下面的图片将显示excel表格,以及我想要实现的表格。
var workbook = Workbook.Load("file.xls");
var worksheet = workbook.Worksheets[0];
var cells = worksheet.Cells;
var dataTable = new DataTable("datatable");
for (int colIndex = cells.FirstColIndex; colIndex <= cells.LastColIndex; colIndex++)
{
dataTable.Columns.Add(cells[0, colIndex].StringValue);
}
for (int rowIndex = cells.FirstRowIndex; rowIndex <= cells.LastRowIndex; rowIndex++)
{
var values = new List<string>();
foreach(var cell in cells.GetRow(rowIndex))
{
values.Add(cell.Value.StringValue);
}
dataTable.LoadDataRow(values.ToArray(), true);
}
这是代码。当我使用这个与提到的.xls我得到“价格:行向左移动。 示例:http://i.imgur.com/QUngL.png 因此,任何有关如何解决这个问题的帮助都非常受欢迎。 感谢。
答案 0 :(得分:1)
以下是某人有同样问题以及如何解决问题的参考链接
How To Resolve Exce.Interop Issue Culture Settings
错误是.NET检查您的线程(C#或VB代码)本地化是否适合您之前安装的MS Excel本地化,如果不是,则表明Microsoft.Office.Interop库已旧或无效。 您的线程本地化源自您的计算机区域设置(来自控制面板 - >区域和语言)
然后有两个选项可以解决这个问题: 1.改变你的线程本地化(通过代码) 2.安装Office的语言包
第一个解决方案是这样的:
using System.Threading; // For setting the Localization of the thread to fit
using System.Globalization; // the of the MS Excel localization, because of the MS bug.
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
excelFileName = System.IO.Path.Combine(excelPath, "Ziperty Buy Model for Web 11_11_2011.xlsm");
答案 1 :(得分:0)
您可以在不打开它的情况下检索它
使用&#34; Excel 12.0 Macro&#34;而不是&#34; Excel 12.0&#34;仅适用于xlsm文件
HDR = YES表示您的Excel表格有标题
Dim myWorkBookPath AS String = "C:\test.xlsx"
Dim mySheetName As String= "Sheet1"
Dim myRangeAddress as String = "A1:K10" 'if you retrieve this by program, think to remove all $ signs . .Replace("$","")
Dim myXlConnectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES""", myWorkBookPath)
Dim myXlConnection = New OleDb.OleDbConnection(myXlConnectionString)
Dim sXlCommand As OleDbCommand = New OleDbCommand(String.Format("Select * FROM [{0}${1}]", mySheetName, myRangeAddress), myXlConnection)
Dim dt = New Data.DataTable()
Dim da = New OleDbDataAdapter(sXlCommand)
myXlConnection.Open()
da.Fill(dt)
myXlConnection.Close()
Return dt
答案 2 :(得分:-1)
放入.cs文件顶部的使用
using System.Reflection;
using Microsoft.Office.Interop.Excel;
// TO USE:
// 1) include COM reference to Microsoft Excel Object library
// add namespace...
// 2) using Excel = Microsoft.Office.Interop.Excel;
private static void Excel_FromDataTable(<strong class="highlight">DataTable</strong> dt)
{
// Create an Excel object and add workbook...
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true); // true for object template???
// Add column headings...
int iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[1, iCol] = c.ColumnName;
}
// for each row of data...
int iRow = 0;
foreach (DataRow r in dt.Rows)
{
iRow++;
// add each row's cell data...
iCol = 0;
foreach (DataColumn c in dt.Columns)
{
iCol++;
excel.Cells[iRow + 1, iCol] = r[c.ColumnName];
}
}
// Global missing reference for objects we are not defining...
object missing = System.Reflection.Missing.Value;
// If wanting to Save the workbook...
workbook.SaveAs("TestExcel.xls",
Excel.XlFileFormat.xlXMLSpreadsheet, missing, missing,
false, false, Excel.XlSaveAsAccessMode.xlNoChange,
missing, missing, missing, missing, missing);
// If wanting to make Excel visible and activate the worksheet...
excel.Visible = true;
Excel.Worksheet worksheet = (Excel.Worksheet)excel.ActiveSheet;
((Excel._Worksheet)worksheet).Activate();
// If wanting excel to shutdown...
((Excel._Application)excel).Quit();
}