PDA

View Full Version : Lập Trình Trên Excel bằng C#



nguyetthu
13-02-2009, 01:03
Mình đang có một số thao tác không giải quyết được rất mong mọi người giúp đỡ:
1. Mình muốn load thông tin lên một sheet đã chỉ định nhưng ko biết dùng thuộc tính nào vì các sách hướng dẫn chỉ có cách mở một file mới trên 1 workbook còn sheet thì không có
2. Mình muốn coppy những range trong 1 sheet vào một sheet khác thì làm như thế nào.
3.Muốn insert những range đó vào CSDL thì làm thế nào?

cuteboy29
18-02-2009, 14:42
/// <summary>
/// Đọc một Sheet và hiển thị nó.
/// </summary>
/// <param name="objExcelSheet">Worksheet được hiển thị</param>
/// <param name="lblErrText">Hiển thị bất kỳ lỗi nào xảy ra.</param>
/// <returns>Trả về giá trị Control chứa thông tin của Worksheet</returns>
public Control ExcelSheetRead(Excel.Worksheet objExcelSheet, Label lblErrText)
{

int nMaxCol = ((Excel.Range)objExcelSheet.UsedRange).EntireColum n.Count;
int nMaxRow = ((Excel.Range)objExcelSheet.UsedRange).EntireRow.C ount;


Table tblOutput = new Table();

TableRow TRow = null;
TableCell TCell = null;

string strSize = "";
int nSizeVal = 0;
bool bMergeCells = false;
int nMergeCellCount = 0;
int nWidth = 0;


if (objExcelSheet == null)
{
return (Control)tblOutput;
}

tblOutput.CellPadding = 0;
tblOutput.CellSpacing = 0;
tblOutput.GridLines = GridLines.Both;


try
{

for (int nRowIndex = 10; nRowIndex <= nMaxRow; nRowIndex++)
{
TRow = null;
TRow = new TableRow();


for (int nColIndex = 1; nColIndex <= nMaxCol; nColIndex++)
{

TCell = null;
TCell = new TableCell();
if (((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Value2 != null)
{

TCell.Text = ((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Text.ToString();
//////////////////////////
if (((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Comment != null)
{
TCell.ForeColor = System.Drawing.Color.Blue;
TCell.ToolTip = ((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Comment.Shape.AlternativeText;
}
else
{
TCell.ForeColor = ConvertExcelColor2DotNetColor(((Excel.Range)objExc elSheet.Cells[nRowIndex, nColIndex]).Font.Color);
}

TCell.BorderWidth = 2;
TCell.Width = 140; //TCell.Width = 40;

//*
TCell.Font.Bold = (bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Bold;
TCell.Font.Italic = (bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Italic;
strSize = ((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Size.ToString();
nSizeVal = Convert.ToInt32(strSize);
TCell.Font.Size = FontUnit.Point(nSizeVal);
TCell.BackColor = ConvertExcelColor2DotNetColor(((Excel.Range)objExc elSheet.Cells[nRowIndex, nColIndex]).Interior.Color);

if ((bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeCells != false)
{
if (bMergeCells == false)
{
TCell.ColumnSpan = (int)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeArea.Columns.Count;
nMergeCellCount = (int)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeArea.Columns.Count;
nMergeCellCount--;
bMergeCells = true;
}
else if (nMergeCellCount == 0)
{
TCell.ColumnSpan = (int)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeArea.Columns.Count;
nMergeCellCount = (int)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeArea.Columns.Count;
nMergeCellCount--;
}
}
else
{
bMergeCells = false;
}

TCell.HorizontalAlign = ExcelHAlign2DotNetHAlign(((Excel.Range)objExcelShe et.Cells[nRowIndex, nColIndex]));
TCell.VerticalAlign = ExcelVAlign2DotNetVAlign(((Excel.Range)objExcelShe et.Cells[nRowIndex, nColIndex]));
TCell.Height = Unit.Point(Decimal.ToInt32(Decimal.Parse((((Excel. Range)objExcelSheet.Cells[nRowIndex, nColIndex]).RowHeight.ToString()))));
nWidth = Decimal.ToInt32(Decimal.Parse((((Excel.Range)objEx celSheet.Cells[nRowIndex, nColIndex]).ColumnWidth.ToString())));
TCell.Width = Unit.Point(nWidth * nWidth);
//*/

}
else
{
if ((bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).MergeCells == false)
{
bMergeCells = false;
}
if (bMergeCells == true)
{
nMergeCellCount--;
continue;
}
TCell.Text = "&nbsp;";
if (((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Comment != null)
{
TCell.ForeColor = System.Drawing.Color.Blue;
TCell.ToolTip = ((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Comment.Shape.AlternativeText;
}
else
{
TCell.ForeColor = ConvertExcelColor2DotNetColor(((Excel.Range)objExc elSheet.Cells[nRowIndex, nColIndex]).Font.Color);
}
TCell.Font.Bold = (bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Bold;
TCell.Font.Italic = (bool)((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Italic;
strSize = ((Excel.Range)objExcelSheet.Cells[nRowIndex, nColIndex]).Font.Size.ToString();
nSizeVal = Convert.ToInt32(strSize);
TCell.Font.Size = FontUnit.Point(nSizeVal);
TCell.BackColor = ConvertExcelColor2DotNetColor(((Excel.Range)objExc elSheet.Cells[nRowIndex, nColIndex]).Interior.Color);

TCell.Height = Unit.Point(Decimal.ToInt32(Decimal.Parse((((Excel. Range)objExcelSheet.Cells[nRowIndex, nColIndex]).RowHeight.ToString()))));
nWidth = Decimal.ToInt32(Decimal.Parse((((Excel.Range)objEx celSheet.Cells[nRowIndex, nColIndex]).ColumnWidth.ToString())));
TCell.Width = Unit.Point(nWidth * nWidth);
}

//TCell.BorderStyle = BorderStyle.Solid;
//TCell.BorderWidth = Unit.Point(1);
//TCell.BorderColor = System.Drawing.Color.Gray;

TRow.Cells.Add(TCell);
}

tblOutput.Rows.Add(TRow);
}
//DataSet ds = new DataSet();
//DataTable dt = new DataTable();
//DataRow dtRows;
//foreach (TableRow row in tblOutput.Rows)
//{
// dtRows = new DataRow();

// for (int i = 0; i < row.Cells.Count; i++)
// {
// dtRows[i] = row.Cells[i].ToString();
// }
// dt.Rows.Add(dtRows);
//}

//GridView1.DataSource = dt.DefaultView;
//GridView1.DataBind();

}
catch (Exception ex)
{
lblErrText.Text = ex.ToString();
}
return (Control)tblOutput;

}

///Tui dang nghien cu về vấn đề bóc tách dữ liệu file Exel, bạn xem và bỏ bớt mấy đoạn Code hiểu thị mầu đi là sài được

nguyetthu
23-02-2009, 14:20
Cảm ơn bạn đã trả lời, ý của tớ là tớ đã tạo sẵn các sheet rùi và tớ muốn load thông tin từ một file xls vào sheet đó. VD nhu file "thongtin.xls" có các tiêu đề như : Mã sinh viên, Họ tên, Ngày Sinh, Điểm Trúng Tuyển thì tớ chỉ muốn load những thông tin về Mã Sinh Vien, Họ Tên và Ngày Sinh lên sheet1 chẳng hạn thôi. Mong bạn giúp tớ. còn đây là địa chỉ yahoo của tớ lenguyetthu1805. Cảm ơn bạn nhiều !

Than Dieu
26-03-2010, 23:34
Xem thử cái này xem có giúp được gì không?

Xử lý excel bằng C# (http://forums.congdongcviet.com/showthread.php?t=13061)

Diễn đàn tốt nhất về C# đây (http://forums.congdongcviet.com/forumdisplay.php?f=9)