You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.3 KiB
61 lines
2.3 KiB
using System;
|
|
using System.Data;
|
|
using System.Reflection;
|
|
using Microsoft.Office.Interop.Excel;
|
|
|
|
namespace SSWMS.Common
|
|
{
|
|
public class ExcelUtils
|
|
{
|
|
public static void ExportDataTable(System.Data.DataTable dt)
|
|
{
|
|
Application application = new Application();
|
|
_Worksheet ws = (_Worksheet)application.Workbooks.Add(Missing.Value).Worksheets[1];
|
|
ws.Name = "导出";
|
|
ws.Cells[1, 1] = "导出日期";
|
|
ws.Cells[1, 2] = StringUtils.GetCurrentDay();
|
|
int iColumn = 1;
|
|
foreach (DataColumn dc in dt.Columns)
|
|
{
|
|
ws.Cells[2, iColumn] = dc.ColumnName;
|
|
++iColumn;
|
|
}
|
|
string[,] asData = new string[dt.Rows.Count, dt.Columns.Count];
|
|
for (int iRow = 0; iRow < dt.Rows.Count; ++iRow)
|
|
{
|
|
for (iColumn = 0; iColumn < dt.Columns.Count; ++iColumn)
|
|
{
|
|
asData[iRow, iColumn] = dt.Rows[iRow][iColumn].ToString();
|
|
}
|
|
}
|
|
ws.get_Range("A3", Missing.Value).get_Resize(dt.Rows.Count, dt.Columns.Count).set_Value(Missing.Value, asData);
|
|
application.Visible = true;
|
|
}
|
|
|
|
public static bool ImportExcel(string sFileName, out string sResult)
|
|
{
|
|
bool bResult = true;
|
|
sResult = string.Empty;
|
|
try
|
|
{
|
|
Application application = new Application();
|
|
Workbook workbook = application.Application.Workbooks.Open(sFileName, Missing.Value, true);
|
|
Worksheet worksheet = (Worksheet)workbook.Worksheets.get_Item(1);
|
|
int iRows = worksheet.UsedRange.Row + worksheet.UsedRange.Rows.Count - 1;
|
|
Range range = worksheet.Cells.get_Range("A1", "C" + iRows.ToString());
|
|
object[,] value2 = (object[,])range.Value2;
|
|
for (int iRow = 1; iRow <= iRows; ++iRow)
|
|
{
|
|
string A = value2[iRow, 1] == null ? string.Empty : value2[iRow, 1].ToString();
|
|
string B = value2[iRow, 2] == null ? string.Empty : value2[iRow, 2].ToString();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
sResult = ex.Message;
|
|
bResult = false;
|
|
}
|
|
return bResult;
|
|
}
|
|
}
|
|
}
|